Tuesday, November 10, 2015

Memory layout of C Program




Above diagram gives an over view of how memory is organized for C program. where all variable(global and local), functions and dynamic memory get stored. 

Why this layout is needed ?

Actually memory layout is just a representation of memory region for different kinds of symbol used in C program. It is needed because variable and kind of memory allocation (static, dynamic)we use in C can not be stored in same place. Based on modifier used for variables compiler decides the memory reason. some of the memory allocate dynamically(on run time), They have separate memory region for allocation.

Detail explanation of each segment ...

Stack 

This is the upper segment of memory layout and this region is used to store all local variables, function calling. This gets cleared once use of that function or local variable is over. So it does not hold value of any its variable for long time. life time of value stored in this region is as long as the function in execution state. All memory and its value automatically gets freed as the function comes out form execution.

Working of stack is last in first out(LIFO). Filling stack depends from machine to machine, in x86 system stack starts growing towards address 0, from top to bottom for above given diagram. 

Heap

Heap is the segment where all dynamic allocation of memory takes place. dynamic allocation of memory is nothing but memory allocation at run time using malloc(), calloc() and realloc(). Heap starts growing from bottom to top, in above figure from BSS to Free space. Filling of heap purely depends on the function we are using for memory allocation melloc(), calloc() or realloc() and it can be freed by using free() function. 

Heap memory segment is very sensitive memory reason any kind of memory corruption can lead to a catastrophe. Programmer should allocate memory as exactly as required and free them as soon as utility is over. Because program does not free the memory automatically like Stack memory region.

BSS(Block Started by Symbol) also called Uninitialized data segment 

This is also called uninitialized memory segment where all global and static uninitialized variable gets saved. In this region all variables get automatically initialized with Zero, if it is not explicitly initialized. 

Data segment or initialized data segment 

Data segment is the region where all the initialized global and static variables get stored. This segment is further divided into two segment 1) Read only initialized segment 2) read-Write initialized segment. 

 In read-only initialized  segment all pointer initialized with string, gets stored in this region, programmer can not change the value at any index. If const key word is used for any variable, that also go to read-only initialized segment only. whereas any global character array initialized with any string, then string values can be changed at any point of time by programmer.

Both can be illustrated by following examples.
 #include <stdio.h>  
 char *temp = "hello";  
 char temp1[] = "yahoo";  
 main()  
 {  
     char *str = "world";  
     temp1[2] = 'd'; // Correct  
     *(temp+2) = 'g'; // Not possible  
     *(str + 2) = 'k'; // Not possible  
     printf("temp = %s\t str = %s \t temp1 = %s\n",temp,str,temp1);  
 }  

In above example three type of initialized taken into consideration. If we complie the code and try to do run it. it will give Segmentation fault . why because  we are trying to change some Constant type of parameters. 

char *temp = "hello" this will be stored in read-only initialized segment(constant segment) . temp1 is simple character array so this will go to read-write initialized data segment.



Text Segment or code segment 

All code syntax and instructions of program is stored in Text segment only. This region is read only, to prevent any over writing by any other overflowed region. 

To check sized used for bss and text we have one command called 'size'.

 $ size a.out   
   text      data      bss      dec      hex    filename  
   1290      574       2      1866      74a    a.out  

References 

https://en.wikipedia.org/wiki/.bss

No comments:

Post a Comment