Monday, 1 April 2019

Memory Layout of C Program


Memory layout of C program contains four segments are text or code segment, data segment, heap segment and stack segment.

Every segment has its own ability to read, write and execute the statement. If a program does not attempt to access memory according to its capability, the segmentation fault is obtained. The program crashes due to the segmentation fault. A segmentation fault occurs when a program tries to read and write an illegal memory location. A dumbed core file is used by the developer to find the reason for a segmentation fault.

Memory Segments



1. Text Segment
2. Data Segment
a. Initialized data segment
b. Uninitialized data segment
3. Heap Segment
4. Stack Segment

Memory layout of c program
Memory layout of C program



























1. Text Segment


  • The text segment is also known as a code segment.
  • This segment contains the binary code of the compiled program.
  • We cannot modify the program abruptly because this segment is a read-only segment.

2.  Data Segment


  • Data of program stores in the Data segment. Data segment divided into two sub-data segments: Initialized data segment and Uninitialized data segment.
  • We can modify the value of the variables during run time of program because this segment is not a read-only segment.


i. Initialized data segment

#include <stdio.h>
int a = 5 //Initialization of global variable 'a' in Data segment
int main(void)
{
static int b = 6; //Initialization of static variable 'b' in Data segment
return 0;
}

ii. Uninitialized data segment


  • Uninitialized data segment stores all uninitialized static and global variables etc.
  • These variables are initialized to zero by default.


#include <stdio.h>
int a; //Uninitialized global variable in the data segment
int main(void)
{
static int b; //Uninitialized static variable in the data segment
return 0;
}

3. Heap Segment

  • Heap segment is part of RAM memory. Memory is allocated during execution of a program. 

  • Dynamic memory is allocated with the help of malloc and realloc function in the C programming language. Heap segment grows in the upward direction as shown in the above figure.


#include <stdio.h>
int main (void)
{
char *ptr = malloc(sizeof(char)*4);
return 0;
}

4. Stack Segment


  • In the stack, elements are stored in last in first out manner (LIFO). Function variables are pushed onto the stack when called and variables are popped from the stack when return. 
  • Whenever a function is called a stack frame is created.
  • Stack pointer tracks the address of the top of the stack.


#include <stdio.h>
int main (void)
{
int a; //variable stored in stack
return 0;
}

The size command helps us to find the size of text, data and bss segments in C. Read the sample of the program given below:


Program 1
#include <stdio.h>
int main(void)
{
return 0;
}

Program 2
In an above program, add global variable.
#include <stdio.h>
int a; //Uninitialized global variable
int main(void)
{
return 0;
}

Program 3
We are adding one static variable in a program as mention below
#include <stdio.h>
int a; //Uninitialized global variable
int main(void)
{
static int b; //Uninitialized static variable
return 0;
}

Program 4
Now, We are going to initialize the static variable in a program and variable is stored in the data segment.
#include <stdio.h>
int a; // Uninitialized global variable
int main(void)
{
static int b = 12; //Initialized static variable 
return 0;
}

Program 5
Initialized the global variable and variable is stored in the data segment.
#include <stdio.h>
int a = 78; //Initialized global variable 
int main(void)
{
static int b = 12; //Initialized static variable
return 0;
}



Recommended Post








































No comments:

Post a Comment