segment because we can modify the stored data later. But, if we write a global variable “const char c = “a”;” in C, then the initialized global data will be stored in the read-only area of the initialized data segment.
Similarly, if the programmer does not initialize the static and global variables, the uninitialized data will be stored in the uninitialized data segment. Please note that the uninitialized data of a program are initialized to zero by the kernel before the program starts execution.
Stack Segment
The stack segment of a program contains all the local variables and function parameters of a function. When we call a function, a stack frame is created in the stack segment. The stack frame typically consists of function parameters, returns address of a function, function local variables, etc. When the function exits, the memory associated with the stack frame is released.
In processors like the x86, the stack frame grows downward.
Heap Segment
When we allocate memory dynamically, the memory is allocated from the heap segment. For example, if we write “n = (int *)malloc(sizeof(int));”, the memory will be allocated from the heap segment.
The heap segment is shared by all the threads, shared libraries, and dynamically loaded modules of a process.






0 Comments