All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.


Total Pageviews

Blog Archive

Follow by Email


Tuesday, 28 February 2017

Memory Layout of a C program

When we run C program it loaded into the ram in a very organized fashion.There are various segments to store code data and code instructions.
The memory layout of a C program consist following sections-
  1. Text or code segment
  2. Initialized Data segment
  3. Uninitialized Data segment or .bss segment
  4. Heap segment 
  5. Stack segment
  6. Command line argumets

Memory layout of a c program

Text or Code segment-

It is the segment in memory which contains executable instructions or compiled program(./a.out or .exe file).

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-

It is basically a data segment which contains all global, static, constant, external (variable with extern keyword) variables that are initialized in the program.
This is not just a read-only segment.For some cases it is a initialized read-only area but for others it is a initialized read-write area.
int a=10;//in read-write area
int const a=10;//in read-only area

Uninitialized data segment or .bss segment-

This segment is also called bss segment, named after an ancient assembler operator that stood for "Block started  by symbol".
This segment lies just above the initialized data segment and consist all the global and sstatic variable that are either initialized to zero or not initialized in the program code.
Variables in this segment are initialized by arithmatic 0 by the kernel.

Heap segment-

Usually Dynamic memory allocation takes place in the heap segment.When memory is allocated using malloc and calloc function than in heap segment allocation will take place.
 It lies just above the bss segment and it grows upward to higher address. 

Stack segment-

 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. Local variables do not appear in data or bss segment. Also all recursive function calls are added to stack. Data is added or removed in a last-in-first-out manner to stack. When a new stack frame needs to be added (as a result of a newly called function), the stack grows downward.

Command line arguments-

This segment lies at the end of memory an used to store the data that is entered at the time of execution of command.
When we pass arguments with command line i.e. passing values with ./a.out 2 3 then these values will be stored in this segment.

Explanation with C program-

memory layout


0 on: "Memory Layout of a C program"