Memory layout / representation of C program is organized in following fashion -
- Text or Code segment
- Initialized data segment
- Uninitialized data segment
- Stack
- Heap
1. Text or Code Segment
Text segment contains machine code of the compiled program. Usually, the text segment is shareable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. The text segment of an executable object file is often read-only segment that prevents a program from being accidentally modifying its instructions.
2. Initialized Data Segment
Initialized data stores all global, static, constant, and external variables ( declared with extern keyword ) that are initialized beforehand. Data segment is not read-only, since the values of the variables can be altered at run time.
This segment can be further classified into initialized read-only area and initialized read-write area.
3. Uninitialized Data Segment (bss)
Data in this segment is initialized to arithmetic 0 before the program starts executing. Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to 0 or do not have explicit initialization in source code.
4. Heap
Heap is the segment where dynamic memory allocation usually takes place. When some more memory need to be allocated using malloc and calloc function, heap grows upward. The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
5. Stack
Stack segment is used to store all local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call is over. Local variables have a scope to the block which they are defined in, they are created when control enters into the block. All recursive function calls are added to stack.
Examples
The size
command reports the sizes (in bytes) of the text, data, and bss segments. These example may be work with gcc compiler only. If you are new to gcc, please checkout this step-wise guide to install gcc and how to compile and run c program.
1. Check the following simple C program
#include <stdio.h>
int main(void)
{
return 0;
}
root@VINEET-PC:~# gcc memory-layout.c -o memory-layout
root@VINEET-PC:~# size memory-layout
text data bss dec hex filename
1115 552 8 1675 68b memory-management
2. Let add one global variable in program, now check the size of bss.
#include <stdio.h>
//Uninitialized variable stored in bss
int global;
int main(void)
{
return 0;
}
root@VINEET-PC:~# gcc memory-layout.c -o memory-layout
root@VINEET-PC:~# size memory-layout
text data bss dec hex filename
1115 552 8 1675 68b memory-management
3. Let us add one static variable which is also stored in bss.
#include <stdio.h>
//Uninitialized variable stored in bss
int global;
int main(void)
{
//Uninitialized static variable stored in bss
static int i;
return 0;
}
root@VINEET-PC:~# gcc memory-layout.c -o memory-layout
root@VINEET-PC:~# size memory-layout
text data bss dec hex filename
1115 552 16 1683 693 memory-management
4. Let us initialize the static variable which will then be stored in Data Segment (DS)
#include <stdio.h>
//Uninitialized variable stored in bss
int global;
int main(void)
{
//Initialized static variable stored in DS
static int i = 100;
return 0;
}
root@VINEET-PC:~# gcc memory-layout.c -o memory-layout
root@VINEET-PC:~# size memory-layout
text data bss dec hex filename
1115 556 12 1683 693 memory-management
5. Let us initialize the global variable which will then be stored in Data Segment (DS)
#include <stdio.h>
//Initialized global variable stored in DS
int global = 10;
int main(void)
{
//Initialized static variable stored in DS
static int i = 100;
return 0;
}
root@VINEET-PC:~# gcc memory-layout.c -o memory-layout
root@VINEET-PC:~# size memory-layout
text data bss dec hex filename
1115 560 8 1683 693 memory-management