Global Layout of Memory Mapping
Global Layout of Memory Mapping
1. TEXT SEGMENT:
The text segment is also known as the code segment.
When we compile any program, it creates an executable file like a.out, .exe, etc., that
gets stored in the text or code section of the RAM memory.
If we store the instructions in the hard disk, then the speed for accessing the
instructions from the hard disk becomes slower as hard disk works on the serial
communication so taking the data from the hard disk will be slower, whereas the
RAM is directly connected to the data and address bus so accessing the data from the
RAM is faster.
2. DATA SECTION:
The data which we use in our program will be stored in the data section.
Since the variables declared inside the main() function are stored in the stack,
but the variables declared outside the main() method will be stored in the data
section.
The variables declared in the data section could be stored in the form of
initialized, uninitialized, and it could be local or global.
Therefore, the data section is divided into four categories, i.e., initialized,
uninitialized, global, or local.
Example:
#include<stdio.h>
int var1;
int var2 = 10;
void function1()
{
printf("I am function1");
}
int main()
{
function1();
return 0;
}
In the above code, var1 and var2 variables are declared outside the main() function
where var1 is the uninitialized variable, whereas the var2 is an initialized variable.
These variables can be accessed anywhere in the program because these variables are
not a part of the main() in the stack.
The uninitialized data segment is also known as a .bss (Block Start Symbol)
segment that stores all the uninitialized global, local and external variables.
If the global, static and external variables are not initialized, they are assigned with
zero value by default.
The .bss segment stands for Block Started by symbol. The bss segment contains
the object file where all the statically allocated variables are stored.
Here, statically allocated objects are those objects without explicit initializations
are initialized with zero value. In the above code, var1 is an uninitialized variable
so it is stored in the uninitialized data section.
#include<stdio.h>
int main()
return 0;
Example:
#include<stdio.h>
char string[] = "javatpoint"; // global variable stored in initialized data segment in
read-write area..
int main()
return 0;
For example: the global variables like char str[] = "javatpoint" and int a=90; will be
stored in the initialized read-write area. If we create the global variable like const char*
string = "javatpoint"; the literal "javatpoint" would be stored in the initialized read area,
whereas the char pointer variable would be stored in the initialized write area.
3. STACK:
When we define a function and call that function then we use the stack frame.
The variables which are declared inside the function are stored in the stack.
The function arguments are also stored in the function as the arguments are also a part
of the function. Such a type of memory allocation is known as static memory
allocation because all the variables are defined in the function, and the size of the
variables is also defined at the compile time.
The stack section plays a very important role in the memory because whenever the
function is called, a new stack frame is created.
4. HEAP
Heap memory is used for the dynamic memory allocation.
Heap memory begins from the end of the uninitialized data segment and grows
upwards to the higher addresses.
The malloc() and calloc() functions are used to allocate the memory in the heap.
The heap memory can be used by all the shared libraries and dynamically loaded
modules.
The free() function is used to deallocate the memory from the heap.
Example:
#include<stdio.h>
int main()
{
return 0;