Memory Layout of C program

Memory Layout In C

Basically, memory layout of C program contains five segments these are the stack segment, heap segment, BSS (block started by symbol), DS (Data segment) and text segment.

Each segment has own read, write and executable permission. If a program tries to access the memory in a way that is not allowed then segmentation fault occurs.

A segmentation fault is a common problem that causes programs to crash. A core file (core dumped file) also associated with segmentation fault that is used by the developer to finding the root cause of the crashing (segmentation fault).

Below find the memory Layout of C Program

 

1. Stack
2. Heap
3. BSS (Uninitialized data segment)
4. DS (Initialized data segment)
5. Text

 

 

Stack:

  • It located at a higher address and grows and shrink opposite to the heap segment.
  • The stack contains local variables from functions and related book-keeping data.
  • A stack frame will create in the stack when a function is called.
  • Each function has one stack frame.
  • Stack frames contain function’s local variables arguments and return value.
  • The stack contains a LIFO structure. Function variables are pushed onto the stack when called and functions variables are popped off the stack when return.
  • SP(stack pointer) register tracks the top of the stack.

 

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

Heap:

  • It is used to allocate the memory at run time.
  • Heap area managed by the memory management functions like malloc, calloc, free etc which may internally 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.
  • It grows and shrinks in the opposite direction of the stack.

 

You can also see below articles,

 

BSS(Uninitialized data segment):

  • It contain all uninitialized global and static variable.
  • All variables in this segment initialized by the zero(0) and pointer with the null pointer.
  • The program loader allocates memory for the BSS section when it loads the program.

 

DS(Initialized data segment):

  • It contains the explicitly initialized global and static variables.
  • The size of this segment is determined by the size of the values in the program’s source code and does not change at run time.
  • It has read-write permission so the value of the variable of this segment can be changed at run time.
  • This segment can be further classified into an initialized read-only area and initialized read-write area.

 

Text:

  • Text segment contains binary of the compiled program.
  • The text segment is a read-only segment that prevents a program from being accidentally modified.
  • It is sharable so that only a single copy needs to be in memory for frequently executed programs such as text editors etc.

The size command basically lists section sizes as well as total size for the input object file. So let see few examples to understand the memory layout of c program.

 

Add a static uninitialized variable

 

Add a static initialized variable

 

Add a global uninitialized variable

 

Add a global and static uninitialized variable



Leave a Reply