What is memory layout of C-program ?

In practical words, when you run any C-program, its executable image is loaded into RAM of computer in an organized manner which is called process address space or Memory layout of C program.

This memeory layout is organized in following fashion:

Text segment

Data segment

Heap segment

Stack segment

Unmapped or reserved

Text segment

Text segment contain executable instructions of your C program, its also called code segment. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system. The text segment is sharable so that only a single copy needs to be in memory for different executing programs, such as text editors, shells, and so on. Usually, text segment is read-only, to prevent a program from accidentally modifying its instructions.

Data segment

There are two sub section of this segment called initialized & uninitialized data segment

Initialized data:- It contains both static and global data that are initialized with non-zero values.This segment can be further classified into read-only area and read-write area.

For example : The global string defined by char string[ ] = “hello world” and a statement like int count=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global statement like const int A=3 makes the variable ‘A’ read-only and to be stored in initialized read-only area.

Uninitialized data (bss segment):- Uninitialized data segment is also called BSS segment. BSS stands for ‘Block Started by Symbol’ named after an ancient assembler operator. Uninitialized data segment contains all global and static variables that are initialized to zero or do not have explicit initialization in source code.

For example : The global variable declared as int A would be stored in uninitialized data segment. A statement like static int X=0 will also stored in this segment cause it initialized with zero.

Heap segment

The heap segment is area where dynamically allocated memory (allocated by malloc(), calloc(), realloc() and new for C++) resides.

When we allocate memory through dynamic allocation techniques(in simple word, run time memory allocation), program acquire space from system and process address space grows that’s why we saw upward arrow indication in figure for Heap.

We can free dynamically allocated memory space (by using Free() or delete). Freed memory goes back to the heap but doesn’t have to be returned to system (it doesn’t have to be returned at all), so unordered malloc/free’s eventually cause heap fragmentation.

When we use dynamic allocation for acquire memory space we must keep track of allocated memory by using its address.

Memory leak error is cause by excess use of dynamic allocation or Heap fragmentation.

Stack segment

The stack segment is area where local variables are stored. By saying local variable means that all those variables which are declared in every function including main( ) in your C program.

When we call any function, stack frame is created and when function returns, stack frame is destroyed including all local variables of that particular function.

Stack frame contain some data like return address, arguments passed to it, local variables, and any other information needed by the invoked function.

A “stack pointer (SP)” keeps track of stack by each push & pop operation onto it, by adjusted stack pointer to next or previous address.

Unmapped or reserved segment

Unmapped or reserved segment contain command line arguments and other program related data like lower address-higher address of executable image, etc.

Click on Next Tab To Understand it Practically