Memory Layout of C Program
Memory Layout :In C after compiling a program, the compiler creates a binary executable file(.exe) .This executable file stores and loads in systematic manner in computer RAM.This Systematic manner is the memory layout of C. It consists of six segments each segments stores the different portion of code.
Detailed explanation of each segment
1. Text/code segment : In object file or memory text segment is one of the sections which contains executable instructions. Moving onwards we know that text segment is also known as Code segment or simply text.
Text segment is used to prevent heaps and stack overflows and overwriting it by placing it below the heap or stack.
2. Initialized data segment : Initialized data segment also known as data segment is present in the virtual memory space of computer.It can store all global,static,constant and external variables.since the values of the variables can be altered at run time data segment is not read-only .
All the variables come under read-write area except const variable where as const variable comes under the read-only area.
Example of Initialized data segment :
3. Uninitialized data segment : Uninitialized data segment stores all the uninitialized global, local and external variables.Another name for the uninitialized data segment is “.bss segment.” The .bss segment stands for Block Started by symbol.
Before the C program executes, the kernel initialized each and every data in bss segment to arithmetic 0 and pointers to null pointer.Static variable are also part of bss segment that are initialized to 0.
Example of Uninitialized data segment :
4. Heap : For dynamic memory allocation Heap memory segment is used.Memory allocation in Heap segment is done using malloc, calloc , realloc and free functions. The heap segment initiates at the end of the BSS segment and grows to larger addresses from there.
Example of Heap :
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others