Friday, March 18, 2016

Memory layout of C program

A typical C program contains the following segments
1) text segment
2) data segment - initialized
3) data segment - unintialized
4) stack
5) Heap


Text segment is the actual c program. The whole C program will be stored in this segment. Actual code is the executable instructions CPU use for execution.
This segment is normally sharable, I mean this section can be shared to editors like vi, vim, ed, shells, and the compilers. Normally this segment will be below the stack or heap.

Data segment: Data segment can be read only or read write. All the global and static variables stored in this segment.
This segment can be divided into initialized data segment where the variables with some values will be stored and uninitialized data segment where variables without initialization will be stored.
Examples:
int a = 100;
char s = 'T' ;
The variables a and s are stored in intialized data segment

char m ;
int i ;
m and i will be stored in uninitialized data segments. This segment is also called BSS(Block started by Symbol)

what about static variables. The memory for static is also stored in data segments.
Example:
static int a = 100;
static float m = 10.34 ;
Note:If static variables will not initiliazed, then they will be set to zero.
So there is no concept of initialized and unitialized data segments for static variables.

Now one more missing section in data segment is READ ONLY.
The global/static variables with read only will be stored in "READ ONLY segment"

Typically data segment can be read only and read-write. All const data variables will be used in READ only data segment and rest in read-write data segments.

Example:
char test[] = "Lets Make Simple" ;
The string literal "Lets Make Simple" is stored in read-only segment while variable test is stored in read-write segment.


STACK segment: As expected used to store the local variables of the function. It contains program stack in LIFO(Last in First Out) fashion. The top of the stack is always the first to be removed from the stack. The register called stack pointer maintain the top of the stack and this will be adjusted as and when function is called and returned. The stack grown opposite to heap area.
Recursion programs use the best use of stack. The stack pointer keeps a seperate copy of each address of called and calling function and hence allocates fresh copy of address for each function call.
int main()
{
   main() ;
}

main() called recursively, as expected we got stack overflow error. Since there is no exit from stack(no exit in the functions).
Typically stack grown from higher address to lower address. Memory for these variables will be de-allocated when the function exits.

HEAP segment: Memory in this segment will be altered when you call malloc, new, free, delete, realloc. They are used to allocate memory dynamically. Heap and stack grows opposite to each other.  Memory in this segment are not freed automatically like stack. You need to call free(), delete() to free the memory from this region.

To know the memory allocated for your program use size command. It shows the size of each segment.

No comments:

Post a Comment