When we execute a C program, the program takes some memory space. In order to manage the memory in a better way, the memory space is divided into four sections called segments. These four segments are:
- Code Segment
- Data Segment
- Stack Segment
- Heap Segment
Each segment has different usage. And, some segments may be shared between different programs.
Code Segment
When we compile a C program, we first get an object file that is later linked with the required headers and libraries by a linker. After linking, we get the executable file. The executable file is a binary file that includes executable instructions. When we load the executable code into memory, the executable instructions of the program are stored in the code segment or the text segment.
This code segment or text segment is often read-only so that the executable instructions of a program do not get changed accidentally. Moreover, the code segment of a program is often shared between multiple instances of the same program.
Data Segment
The data segment of a program contains all the global and static variables of a program. These global and static variables can be initialized or uninitialized. The initialized global and static variables are stored in the initialized data segment. The initialized data segment can be read-only or read-write.
For example, if we write “static int i = 10;” in C, the data will be stored in the read-write area of the initialized data …
0 Comments