Friday, November 7, 2008

Memory layout of C program

Memory organization of a C program:

Code Segment :

The code segment is the place where the executable program is stored. The other name for code segment is text segment as it stores the machine instructions.

Data Segment:

Just above the code segment is the data segment which stores the static and global variables. The data segment itself is divided into two parts: Initialized data segment and un-initialized portion also called as BSS(Block Starting with Symbol).
Initialized global and static variables are placed inside the initialized data segment and un-initialized global variables are stored in BSS with default value Zero. The complete BSS is made zero by the start-up routine even before calling the main().

Stack Segment :

The stack segment is used to store local and automatic variables. If un-initialized the default value will garbage value. The system stack grows downwards as opposed to heap.

Heap Segment :

Just above the stack segment is the heap segment which is used for dynamic memory allocation using malloc() and calloc().
The heap grows upwards in direction.

All the segments are in the user space of the operating system in the RAM.

1 comment:

catherine said...

Heap does not exist in all controllers