Different Sections of a Process is:
Stack Segment:
- The stack sits at the higher address and grows towards the lower address.
- It is unique for each process and stores store automatic variables (non-static local variables).
- A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack.
- When a function is called, a stack frame (or a procedure activation record) is created and PUSHed onto the top of the stack.
- This stack frame contains information such as the address from which the function was called and where to jump back to when the function is finished (return address), parameters, local variables, and any other information needed by the invoked function.
- Data is removed in a last-in-first-out manner from the stack.
Heap Segment:
- This segment is responsible for holding all the variables are which are dynamically allocated via malloc(), calloc(), realloc() and new for C++ function.
- This is shared by all the shared libraries and processes.
- The stack and heap are traditionally located at opposite ends of the process’s virtual address space.
- It is typical for the heap to grow upward. This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items.
- It is also typical for the heap to start immediately after the BSS read of the data segment.
- The end of the heap is marked by a pointer known as the break.
- A program will terminate if they reference past the break.
Data Segment:
- This section of memory is responsible for holding global variables, static variables, constant and extern variables.
- This is further divided into two parts:
- Initialized data segment: This contains all the global, static, extern, and constant variables which are initialized with non-zero values.
- char a[] = “hello” is initialized read write area and char *a= “hello” is initialized read only area.
- Uninitialized data segment: This is also called as bss(block starting symbol) segment and contains all the global, static, extern, and constant variables which are uninitialized.
- It contains all global variables and static variables that are initialized to 0 or do not have explicit initialization in source code
Text Segment:
- This is also referred to as a code segment and contains the executable and usually starts from a low address and is a static and read-only part of memory.
- 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
Note:
We also have one segment called the command line arguments section which holds the variable passed as a command-line argument. int main( int argc, int *argv[])
Diag-1: Process memory layout

Related Posts:
- Process vs program
- States of a process
- Process Data structure (PCB)
- Attributes of a process
- Context switching
- Termination of a process
Reference:
https://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html
Categories: Operating system (OS)
Leave a Reply