FOR FREE YEAR SOLVED

Memory layout for a process:

 

 

Text/Code segment: Text segment contains executable instructions of a program. It is a read-only section because code cannot be changed or modified during execution.

 

Generally, code segments allow sharing a single code i.e. more than one process can access a single sharable code in memory and the advantage is that only a single copy needs to memory i.e. no need for extra memory for this sharable code for all programs.

 

Data segment: This Segment contains a static and global variable of the program. 

Example:  int static sum=0.

 

Below the data segment, there is another segment called "Block Started by Symbol"(BSS) which store uninitialized static variables. Example: int static sum.

 

Stack segment: This segment contains part of the program which uses the stack as temporary usage.

This area is dedicated to store all the variables required by a function call in a program.

 

For every function of the program, there is a stack frame that contains local variables as well as actual parameters of the function.

 

So, variables of all functions are completely separated by individual stack frames.

 

The same way the recursion functions are implemented. Each time a recursion function calls itself, new stack frame is allocated on the top of the stack.

 

Heap segment: This segment contains that part of the program where dynamic memory allocation is used. Example: Malloc and Alloc in C.