Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
C Programming Language Tutorial
Next article icon

Memory Layout of C Programs

Last Updated :13 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The memory layout of a program refers to how the program’s data is stored in the computer memory during its execution. Understanding this layout helps developers manage memory more efficiently and avoid issues such as segmentation faults and memory leaks.

A C program's memory is organized into specific regions (segments) as shown in the below image, each serving distinct purposes for program execution.

Memory-Layout-of-C-Program
Different Segments in C Program's Memory

1. Text Segment

Thetext segment(also known as code segment) is where the executable code of the program is stored. It contains the compiled machine code of the program's functions and instructions. This segment is usually read-only and stored in the lower parts of the memory to prevent accidental modification of the code while the program is running.

The size of the text segment is determined by the number of instructions and the complexity of the program.

2. Data Segment

Thedata segment stores global and static variables that are created by the programmer. It is present just above the code segment of the program. It can be further divided into two parts:

A. Initialized Data Segment

As the name suggests, it is the part of the data segment that contains global and static variables that have been initialized by the programmer. For example,

C
// Global variableinta=10;// Static variablestaticintb=20;

The above variables a and b will be stored in the Initialized Data Segment.

B. Uninitialized Data Segment (BSS)

Uninitialized data segment often called the "bss" segment, named after an ancient assembler operator, that stood for "Block Started by Symbol" contains global and static variables that are not initialized by the programmer. These variables are automatically initialized to zero at runtime by the operating system. For example, the below shown variables will be stored in this segment:

C
// Global variableinta;// Static variablestaticint;

3. Heap Segment

Heap segment is where dynamic memory allocation usually takes place. The heap area begins at the end of the BSS segment and grows towards the larger addresses from there. It is managed by functions such as malloc(),realloc(), andfree() which in turn may use the brk and sbrk system calls to adjust its size.

The heap segment is shared by all shared libraries and dynamically loaded modules in a process. For example, the variable pointed byptrwill be stored in the heap segment:

C
#include<stdio.h>intmain(){// Create an integer pointerint*ptr=(int*)malloc(sizeof(int)*10);return0;}

4. Stack Segment

Thestack is a region of memory used forlocal variables and function call management. Each time a function is called, astack frame is created to store local variables, function parameters, and return addresses. This stack frame is stored in this segment.

The stack segment is generally located in the higher addresses of the memory and grows opposite to heap. They adjoin each other so when stack and heap pointer meet, free memory of the program is said to be exhausted.

Example of data stored in stack segment:

C
#include<stdio.h>voidfunc(){// Stored in the stackintlocal_var=10;}intmain(){func();return0;}

Practical Examples

The size(1) command in MinGW reports the sizes (in bytes) of the text, data, and bss segments of a binary file.

1. Check the following simple C program 

C
#include<stdio.h>intmain(){return0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 248 8 1216 4c0 memory-layout

2. Let us add one global variable in the program, now check the size of bss

C
#include<stdio.h>// Uninitialized variable stored in bssintglobal;intmain(){return0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 24812 1220 4c4 memory-layout

3. Let us add one static variable which is also stored in bss.

C
#include<stdio.h>// Uninitialized variable stored in bssintglobal;intmain(){// Uninitialized static variable stored in bssstaticinti;return0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 24816 1224 4c8 memory-layout

4. Let us initialize the static variable which will then be stored in the Data Segment (DS)

C
#include<stdio.h>// Uninitialized variable stored in bssintglobal;intmain(void){// Initialized static variable stored in DSstaticinti=100;return0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960252 12 1224 4c8 memory-layout

5. Let us initialize the global variable which will then be stored in the Data Segment (DS)

C
#include<stdio.h>// initialized global variable stored in DSintglobal=10;intmain(){// Initialized static variable stored in DSstaticinti=100;return0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960256 8 1224 4c8 memory-layout

Example to Verify the Memory Layout

C
#include<stdio.h>#include<stdlib.h>// Global variableintgvar=66;// Constant global variableconstintcgvar=1010;// uninitialized global variableintugvar;voidfoo(){// Local variableintlvar=1;printf("Address of lvar:\t%p",(void*)&lvar);}intmain(){// Heap variableint*hvar=(int*)malloc(sizeof(int));// Checking and comparing address of different// elements of program that should be stored in// different segements of the memoryprintf("Address of foo:\t\t%p\n",(void*)&foo);printf("Address of cgvar:\t%p\n",(void*)&cgvar);printf("Address of gvar:\t%p\n",(void*)&gvar);printf("Address of ugvar:\t%p\n",(void*)&ugvar);printf("Address of hvar:\t%p\n",(void*)hvar);foo();return0;}


Output

Address of foo:         0x60d723996189
Address of cgvar: 0x60d723997004
Address of gvar: 0x60d723999010
Address of ugvar: 0x60d723999018
Address of hvar: 0x60d73b9072a0
Address of lvar: 0x7ffd0e85e0c4

Comparing above addresses, we can see than it roughly matches the memory layout discussed above.


Memory Structure of a Program
Visit Courseexplore course icon

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp