0% found this document useful (0 votes)
22 views5 pages

Global Layout of Memory Mapping

The document outlines the global layout of memory mapping in a C program, detailing the various sections including the text segment, data section (initialized and uninitialized), stack, and heap. It explains the purpose of each segment, how variables are stored, and provides examples for better understanding. The text segment contains executable instructions, the data section stores variables, the stack manages function calls, and the heap is used for dynamic memory allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Global Layout of Memory Mapping

The document outlines the global layout of memory mapping in a C program, detailing the various sections including the text segment, data section (initialized and uninitialized), stack, and heap. It explains the purpose of each segment, how variables are stored, and provides examples for better understanding. The text segment contains executable instructions, the data section stores variables, the stack manages function calls, and the heap is used for dynamic memory allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

GLOBAL LAYOUT OF MEMORY MAPPING

A typical memory representation of a C program consists of the following sections.


1. Text segment (i.e. instructions)
2. Initialized data segment
3. Uninitialized data segment (block starting symbol)
4. Heap
5. Stack

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 data section consists of two segments:


Uninitialized data segment
Initialized data segment

Uninitialized Data Segment

 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.

Example of uninitialized data segment:

#include<stdio.h>

char a; // uninitialized global variable..

int main()

static int a; // uninitialized static variable..

return 0;

Initialized Data Segment

 An initialized data segment is also known as the data segment.


 A data segment is a virtual address space of a program that contains all the global and
static variables which are explicitly initialized by the programmer.
 The values of variables in a data segment are not read-one, i.e., they can be modified
at run time. This data segment can be further classified into categories:

o Initialized read-only area: It is an area where the values of variables cannot be


modified.
o Initialized read-write area: It is an area where the values of variables can also be
altered.

Example:

#include<stdio.h>
char string[] = "javatpoint"; // global variable stored in initialized data segment in
read-write area..

int main()

static int i = 90; // static variable stored in initialized data segment..

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()
{

int *ptr = (int*)malloc(sizeof(int)) ; // memory gets allocated in the heap segment.

return 0;

You might also like