CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 3 min read

Memory parts

In plain words: The memory of a process is split into parts, called sections or segments, each with a different job. Five are worth knowing.

Part Holds Size
Text The machine instructions (read-only) Fixed at load time
Data Global and static variables that have a starting value Fixed at load time
BSS Global and static variables with no starting value (they begin as zero) Fixed at load time
Heap Memory requested at run time with malloc Grows and shrinks
Stack Function arguments, local variables and return addresses Grows and shrinks

The name BSS is a leftover from an old assembler instruction. It matters for one practical reason: a variable in BSS takes no space in the program file, because the file only needs to record "this many zero bytes".

The typical layout puts text at the lowest addresses, then data and BSS, then the heap above them. The stack sits at the top and grows down. The heap grows up, toward the stack. The operating system makes sure they never meet.

The program below prints one address from each part:

#include <stdio.h>
#include <stdlib.h>

int initialised = 15;        /* data */
int uninitialised;           /* bss */

int main(int argc, char *argv[]) {
    int local = 1;
    int *heap = malloc(100);
    printf("code  (main)         %p\n", (void *)main);
    printf("data  (initialised)  %p\n", (void *)&initialised);
    printf("bss   (uninit)       %p\n", (void *)&uninitialised);
    printf("heap  (malloc)       %p\n", (void *)heap);
    printf("stack (local)        %p\n", (void *)&local);
    printf("stack (argv strings) %p\n", (void *)argv[0]);
    free(heap);
    return 0;
}

Compiled with gcc -no-pie so that the program loads at a fixed place, the test machine printed:

code  (main)         0x401196
data  (initialised)  0x404030
bss   (uninit)       0x404044
heap  (malloc)       0x3a8212a0
stack (local)        0x7fff53ead96c
stack (argv strings) 0x7fff53eae756

The order matches the picture: code is lowest, data and BSS follow, the heap is higher, and the stack is far above at the 0x7fff... end. The command-line strings (argv) sit at the very top, above the stack frames. The heap and stack addresses change from run to run, because the system places them at slightly random spots as a security measure (Level 10).

The command size reports the first three parts of a program file. On the test machine, size m1 gave text 1,733, data 584 and bss 8 bytes, and the total is 1,733 + 584 + 8 = 2,325.

Stack and heap

The stack is the region of a process's memory that holds one frame for each active function call (its arguments, local variables and saved return address); a frame is pushed when the function is called and popped when it returns. The heap is the region from which the program obtains blocks of memory at run time with malloc; a block stays allocated until the program frees it.

Example: a local int lives on the stack and disappears when its function returns, while a malloc(100) block stays until free is called.

Check yourself: 1. In which part is the global int count = 7;? 2. In which part is a local variable inside main? 3. Which two parts change size while the program runs?