Fundamentals 25 min read

Mastering C Memory Layout: Stacks, Heaps, Segments, and const Usage

This article explains the five memory partitions in C, the storage regions created during compilation and linking, the different program segments (code, RO data, RW data, BSS, heap, stack), the proper use of const and pointers, 8051-specific memory qualifiers, and a detailed comparison of stack and heap allocation, including practical code examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering C Memory Layout: Stacks, Heaps, Segments, and const Usage

Memory Regions in C Programs

At runtime a C program uses five logical memory regions: stack , heap , free‑storage (memory obtained with malloc / calloc and released with free), global/static storage , and constant (read‑only) storage . The stack holds automatic variables and function parameters; the heap holds blocks allocated with malloc, calloc or new; free‑storage is similar to the heap but is explicitly freed; global/static variables share a single area; constant objects reside in a read‑only region.

Executable Sections and Runtime Segments

Compiling a C source file goes through compilation, assembly and linking. The resulting executable contains several sections that become runtime segments:

Code (Text) Segment – machine instructions.

Read‑Only Data (RO) Segment – string literals, const objects.

Initialized Read‑Write Data (RW) Segment – global/static variables with explicit initializers.

Uninitialized Data (BSS) Segment – global/static variables without initializers; occupies no space in the file but is zero‑filled at program start.

Heap – memory obtained at runtime via malloc, calloc, new.

Stack – automatic storage for locals, parameters and return addresses.

Illustrative Code

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

Correct Use of const

The const qualifier places an object in the RO segment and forbids modification of its value. For pointers the position of const determines what is immutable: const int *p – the pointed‑to data is read‑only, the pointer itself may change. int *const p – the pointer is read‑only, the pointed data may be modified. const int *const p – both pointer and data are read‑only.

Attempting to modify a string literal, e.g. char *p = "tiger"; p[1] = 'I';, triggers a segmentation fault because the literal resides in RO memory.

8051‑Specific Storage Classes

Embedded C for the 8051 family adds the following storage classes: data – 128 bytes of directly addressable internal RAM (fastest). idata – 256 bytes of internal RAM accessed via pointers. xdata – external RAM up to 64 KB accessed with the DPTR register. pdata – the low 256 bytes of external RAM, accessed with special instructions. code – places variables in program memory (ROM/Flash), making them read‑only.

Bit‑addressable variables can be defined with sbit:

char bdata MODE;
sbit MODE_7 = MODE^7;
sbit MODE_6 = MODE^6;
/* … up to MODE_0 */

Stack vs. Heap: Allocation, Limits and Performance

Allocation method : The stack is managed automatically for each function call; the heap requires explicit malloc / free (or new / delete).

Size limits : Stack size is fixed per thread (overflow raises a stack‑overflow exception). Heap size is limited only by available virtual memory.

Efficiency : Stack allocation is essentially a pointer adjustment and is therefore extremely fast. Heap allocation involves bookkeeping, possible block splitting and fragmentation, and is slower.

Content :

Stack stores function parameters, local variables and return addresses (LIFO order).

Heap stores dynamically allocated objects managed as a linked list of free blocks.

Memory layout diagram
Memory layout diagram

Summary

Understanding the five memory partitions, the mapping between executable sections and runtime segments, and the proper use of const and 8051‑specific qualifiers enables developers to write safe and efficient C code, especially on resource‑constrained embedded platforms.

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 programmingHeapData Segments
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.