Memory Layout of C Program

Memory Layout :

In  C after compiling a program, the compiler creates a binary executable file(.exe) .This executable file stores and loads in systematic manner in computer RAM.This Systematic manner is the memory layout of C. It consists of six segments each segments stores the different portion of code.
Memory Layout of C

Memory Layout of C consists of following segments:

  1. Text/code segment
  2. Initialized data segment 
  3. Uninitialized data segment 
  4. Heap 
  5. Stack 
  6. Command-line arguments
Detailed Memory Layout of C

Detailed explanation of each segment

1. Text/code segment : In object file or memory text segment is one of the sections which contains executable instructions. Moving onwards we know that text segment is also known as Code segment or simply text.
Text segment is used to prevent heaps and stack overflows and overwriting it by placing it below the heap or stack.

2. Initialized data segment : Initialized data segment also known as data segment is present in the virtual memory space of computer.It can store all global,static,constant and external variables.since the values of the variables can be altered at run time data segment is not read-only .
All the variables come under read-write area except const variable where as const variable comes under the read-only area.

Example of Initialized data segment :

Run
#include <stdio.h>
char a[] = "Anurag";		/* read-write area */
const char p[] = "PrepInsta";	/* read-only area */
int main ()
{
  static int j = 15;
  return 0;
}

3. Uninitialized data segment : Uninitialized data segment stores all the uninitialized global, local and external variables.Another name for the uninitialized data segment is “.bss segment.” The .bss segment stands for Block Started by symbol.
Before the C program executes, the kernel initialized each and every data in bss segment to arithmetic 0 and pointers to null pointer.Static variable are also part of bss segment that are initialized to 0.

Example of Uninitialized data segment :

Run
#include <stdio.h >
char p;				/*  variable(Uninitialized) stored in bss */
int main ()
{
  static int q;			/*  variable stored(Uninitialized static) in bss */
  return 0;
}

4. Heap : For dynamic memory allocation Heap memory segment is used.Memory allocation in Heap segment is done  using malloc, calloc , realloc and free  functions. The heap segment initiates at the end of the BSS segment and grows to larger addresses from there.

Example of Heap :

Run
#include<stdio.h>
int main ()
{
  int *st = (int *) malloc (sizeof (int));	//heap memory is allocated.
}
5. Stack : Stack segment grows in opposite  to heap . It uses Last-In-First-Out (LIFO)  manner  to store local variables. All recursive functions uses stack for implementation.Recursive functions are those which calls itself again and  again. Stack plays an important role in the memory as a new stack frame is created every time a function is called as it is used for passing arguments to functions along with return address of instructions.  

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription