Fundamentals 25 min read

Mastering C Memory Layout: Stack, Heap, and Data Segments Explained

This article provides a comprehensive guide to C memory organization, covering the five primary memory regions, the layout of compiled C programs, detailed segment types such as code, read‑only, read‑write, BSS, heap and stack, as well as const usage, pointer rules, and embedded‑system storage qualifiers.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering C Memory Layout: Stack, Heap, and Data Segments Explained

1. Five Memory Regions

The memory of a C program is divided into five areas: stack (automatic variables and function parameters), heap (dynamic allocation via malloc / new), free store (similar to heap but released with free), global/static storage (global and static variables), and constant storage (read‑only literals).

2. C Program Storage Layout

A C source file is transformed through compilation, assembly, and linking into an executable that contains several sections. At run time the program occupies the following regions:

Code (Text) Segment : machine instructions.

Read‑Only Data Segment (RO) : const variables and string literals.

Read‑Write Data Segment (RW) : initialized global/static variables.

Uninitialized Data Segment (BSS) : global/static variables without explicit initializers.

Heap : memory obtained with malloc / new.

Stack : automatic variables, function arguments, return addresses.

3. Detailed Segment Description

During linking the code, RO, and RW segments are created. The BSS segment is allocated at program start. The heap and stack are created dynamically when the program runs. The following diagram illustrates the layout (image omitted for brevity).

4. Const Usage and Pointer Rules

The const qualifier places the variable in the read‑only segment. Its position relative to the asterisk determines what is constant: const int *p: the pointed‑to value is read‑only, the pointer itself is mutable. int *const p: the pointer is read‑only, the pointed‑to value can change. const int *const p: both pointer and pointed‑to value are immutable.

Typical misuse, such as incrementing a const char * that points to a string literal, results in a compilation error because the literal resides in the RO segment.

5. Embedded‑C Storage Qualifiers (8051)

For 8051 microcontrollers the keywords data, idata, xdata, and pdata control where a variable is placed: data: internal RAM (0x00‑0x7F), fastest access. idata: internal RAM accessed via pointers (0x00‑0xFF). xdata: external RAM or flash accessed with DPTR. pdata: lower 256 bytes of external RAM, accessed with bit‑addressable syntax.

Using code places data in ROM (read‑only), while data places it in RAM (read‑write).

6. Heap vs. Stack Differences

Allocation : Stack memory is allocated automatically by the compiler; heap memory must be requested explicitly with malloc / new and freed manually.

Size Limits : Stack size is fixed and limited; heap size is limited only by available virtual memory.

Performance : Stack allocation/deallocation is very fast; heap operations are slower and can cause fragmentation.

Content : Stack holds local variables, function parameters, and return addresses; heap holds dynamically allocated objects.

int main(){
    short b;               // stack variable (2 bytes)
    char a[100];           // stack array (100 bytes)
    char s[]="abcde";     // s on stack, literal in RO segment
    char *p1;               // pointer on stack
    char *p2="123456";    // pointer on stack, literal in RO segment
    static char bss_2[100];// local uninitialized static (BSS)
    static int c=0;        // local initialized static (RW)
    p1 = (char *)malloc(10*sizeof(char)); // heap allocation
    strcpy(p1,"xxx");
    free(p1);
    return 0;
}

7. Practical Example of Const and Pointers

typedef char* pStr;
int main(){
    char string[6] = "tiger";
    const char *p1 = string;   // pointer to const data, pointer mutable
    const pStr p2 = string;    // pointer itself const, data mutable
    p1++;   // OK
    // p2++; // error: increment of read‑only variable
    printf("p1=%s p2=%s
", p1, p2);
    return 0;
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

StackconstC programmingHeap
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.