To understand the way our C program works, we need to understand the arrangement of the memory assigned to our program.
All the variables, functions, and data structures are allocated memory into a special memory segment known as Data Segment. The data segment is mainly divided into four different parts which are specifically allocated to different types of data defined in our C program.
The parts of Data segments are :
1. Data Area
It is the permanent memory area. All static and external variables are stored in the data area. The variables which are stored in the data area exist until the program exits.
2. Code Area
It is the memory area which can only be accessed by the function pointers. The size of the code area is fixed.
3. Heap Area
As we know that C supports dynamic memory allocation. C provides the functions like malloc() and calloc() which are used to allocate the memory dynamically. Therefore, the heap area is used to store the data structures which are created by using dynamic memory allocation. The size of the heap area is variable and depends upon the free space in the memory.
4. Stack Area
Stack area is divided into two parts namely: initialize and non-initialize. Initialize variables are given priority than non-initialize variables.
- All the automatic variables get memory into stack area.
- Constants in c get stored in the stack area.
- All the local variables of the default storage class get stored in the stack area.
- Function parameters and return value get stored in the stack area.
- Stack area is the temporary memory area as the variables stored in the stack area are deleted whenever the program reaches out of scope.