Memory layout of C program in linux
Learn memory layout of C program such as text segment, heap segment, stack segment, command line arguments in linux c with examples.
Memory layout of a C program
Let us understand the memory layout of running c program.
Text Segment is also named as text and it contains executable instructions. This is the lower address occupied by the program in memory. Usually, the text segment is sharable 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. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
Initialized Data Segment is also named as data. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.
Uninitialized Data Segment is also named as bss. uninitialized data starts at the end of the data segment and contains all global variables and static variables that do not have explicit initialization in source code.
For instance a variable declared static int i; would be contained in the BSS segment.
Heap is the segment where dynamic memory allocation usually takes place.
The heap area begins at the end of the BSS segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
Stack is where automatic and temporary variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.
A stack pointer register tracks the top of the stack; it is adjusted each time a value is pushed onto the stack. The set of values pushed for one function call is termed a stack frame; A stack frame consists at minimum of a return address.
The difference between program counter and stack pointer is that the program counter is a register which holds the address of next instruction when the current instruction is under execution while stack pointer is a pointer which points to the top element of the stack.
Command Line arguments
This is the highest address occupied by the program in memory.
C program to demonstrate memory layout
Let's demonstrate the memory layout of a C program using few example programs. To begin with, let's write a simple c program to add two integers.
int p1 = 10,p2 = 20;
result1 = add(p1,p2);
printf("The result is %d\n",result1);
int add(int a,int b)
The result is 30
//size command output
# size ./a.out
text data bss dec hex filename
830 292 8 1130 46a ./a.out
To Read Full Article
You need to subscribe & logged in
Want to contribute a new article? Write and upload your article information here