Do you know the architecture of memory and how programmers access memory? The C program memory layout can be divided into five segments or sections. Without beating around the bush, let us jump right into the main topic.
The five segments are:
- Text Segment
- Initialized Data Segment
- Uninitialized Data Segments
- Stack Segment
- Heap Segment
What is Code Segment?
This segment is also known as the text segment. This Segment of a C program contains the binary file of the program which is created after compilation. This text segment does not allow users to modify it which means it is read-only. This prevents tampering of the program after its compilation. This text segment can be shared.
Also Read: What are Storage Classes in C Language?
Initialized Data Segment
This segment of the C program memory layout contains initialized global and static variables are that explicit. The segment size is directly proportional to the size of the values. The segment can be modified at run time as it has read-write permission.
int x=10; // After initializing the Value of x is stored in initialized Data segment as shown in the above figure.
static int y=3;
Uninitialized Data Segment in C
As the name suggests, this segment contains every uninitialized global and static variable. The variables in this segment are initialized with 0, while the pointers in this segment are initialized with NULL. The memory for this section is allocated when the program loads.
int x; //‘x’ is uninitialized variable. This is stored in BSS. Variable is not initialized, so, it is set to zero.
static int y;
What is Heap Segment?
Heap is used for dynamic memory allocation at run time. The heap segment begins when Uninitialized Data Segment comes to an end. It is managed by functions like malloc, realloc, and free. These functions may use brk and sbrk system calls for size adjustment. The heap segment is shared by all the shared libraries.
Also Read: Implementation of Sizeof and strlen in C
char *p1=malloc(sizeof(char)*10); //Allocated by Heap through malloc (). This deallocated by using free() function.
What is Stack Segment?
The stack segment contains a Last In First Out structure of the program stack and is located in the higher parts of the memory.
A register called Stack Pointer store the top of the address. Whenever a value is pop from the stack, the stack pointer is adjusted. All the values that are pop out in a function call are collectively known as Stack Frame.
Frequently Asked Questions (FAQs)
Q1) What is the BSS in C program memory layout?
The Uninitialized Data Segment which we studied in this article is also referred to as BSS.
Q2) Where are static variables stored in C?
If you initialize the static variable, then those variables are stored in the data segment.
In case, they are not initialized (initialized to 0), they are stored in BSS.
Q3) What is a segmentation fault?
Segmentation fault/ error is an error that occurs when you try to access a memory that doesn’t allow access. Example: If you try to write the Text Segment, it will show a core dump or segmentation error because the text segment is write-protected.
Q4) Where are macros stored in-memory layout of the C program?
Macros are managed by preprocessors. So the declared macros are replaced by their respective definitions before the compilation process starts. Hence, they are not stored in memory. But the code for macros just repeats whenever needed. So macros can take up a bit of memory and we cannot determine the amount of memory.
Q5) What is the difference between Data Segment and Stack Segment in the C memory layout?
The data segment is used to store global and static variables, while the Stack segment is used to store automatic variables that are variables local to a function. During function calls, a part of the stack is allocated to store function information.
Q6) In which memory segment is the const variable stored?
The const variables are stored in the Initialized Data Segment (DS) of the main memory in C.
Q7) Why is the RAM/memory layout of a program divided into different sections? Why not just memory?
This is done for better organization of various parts of the program into different sections. A stack is needed to store information about function calls heap is needed for temporary storage, etc. Every section serves a purpose.
Q8) What is the difference between Program and Data Memory?
Program is the set of instructions executed by the CPU while data is information that is used by the program.
PROGRAM MEMORY: Program memory stores the constants and is write-protected which means you can only read this memory.
DATA MEMORY: This memory is used to store variables that can be modified.
Q9) What is the difference between stack and heap?
While both stack and heap are used for memory allocation, it should be noted that heap is used for dynamic memory allocation. The variables that are stored on the stack have faster access. This allocation is done at the compilation stage. Also, the stack follows a LIFO (Last in First Out) structure.
Heap memory is dynamic and is lifted after a while. The allocation process is carried out during run time and the variables stored on the heap are a bit slower to access.