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

Memory Layout of a C Program

The document explains the memory layout of a C program, detailing five main segments: Text, Data (further divided into Initialized and Uninitialized), Stack, and Heap. It illustrates how different types of variables are allocated in these segments, particularly focusing on the BSS segment for uninitialized variables and the Data segment for initialized variables. The document also provides examples of how changes in variable initialization affect memory allocation in these segments.

Uploaded by

albertarjun1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Memory Layout of a C Program

The document explains the memory layout of a C program, detailing five main segments: Text, Data (further divided into Initialized and Uninitialized), Stack, and Heap. It illustrates how different types of variables are allocated in these segments, particularly focusing on the BSS segment for uninitialized variables and the Data segment for initialized variables. The document also provides examples of how changes in variable initialization affect memory allocation in these segments.

Uploaded by

albertarjun1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

🧠 Memory Layout of a C Program

❖ Understanding how memory is allocated during the execution of a C


program
When a C program is compiled and run, it doesn’t get loaded into just
one block of memory. Instead, different parts of the program are stored in
different sections of memory, each serving a specific purpose.
▪ Memory Segments in a C Program
A typical C program’s memory layout is divided into five main segments:
1. Text (Code) Segment

• Stores the compiled machine code of the program.


• It is read-only to prevent accidental modification of instructions
during execution.
2. Data Segment

This is further divided into two parts:

a. Initialized Data Segment


• Contains initialized global, static, and external variables.
• Also stores constant global variables.
Divided into:
• Read-Only Section: For constants (const int x = 10;)
• Read-Write Section: For modifiable variables (int x = 10;)
b. Uninitialized Data Segment (BSS Segment)
• Stores uninitialized global and static variables.
• Also includes constant global variables that are not initialized.
💡 Fun fact: BSS stands for Block Started by Symbol.

3. Stack Segment

Used for function call management.


Stores:
• Local variables (auto) → i.e., regular local variables defined
inside functions without the static keyword.
• Function parameters
• Return addresses (for tracking which function to return to)

• Grows downward in memory (from higher to lower addresses).


• Managed automatically via push/pop operations.

4. Heap Segment

• Used for dynamic memory allocation (malloc(), calloc(),etc.).


• Grows upward (from low to high memory).
• Requires manual memory management using free().
Let’s begin with a very simple program:

This program only contains a main() function and a simple return 0;


statement. It doesn’t use any variables or perform any logic — just the
minimum needed to compile and run.
Let’s check the size of the program:

We can see that the data segment contains 1532 bytes and the BSS
segment contains 112 bytes.
Now let’s change the code and check the size again:

In this code, we have added one line:


static int i;
a static variable declared in global scope, and it's not initialized.
Let’s check the size of the program now:

The BSS segment increased by 4 bytes because a static, uninitialized


variable i was added. Static or global variables that are not explicitly
initialized are stored in the BSS segment, which is reserved for such
uninitialized data. Since i is of type int, it typically occupies 4 bytes on most
systems. The text and data segments remained unchanged, confirming that
uninitialized static or global variables are allocated in the BSS segment, not
in the stack or heap.
What Happens When We Initialize a Static Global Variable?
Let’s slightly change the previous code. This time, we initialize the static
variable with a value is equal to Zero :

• Since it's now initialized, we expect it to move from the BSS segment
to the data segment, right?

🔍 Let’s check the size again:


⚠️ Wait! The BSS segment size didn’t change, and the data segment size
also stayed the same.

Why is that?

Even though we explicitly initialized i = 0, the compiler treats this the


same as not initializing at all, because:

• All global/static variables in C are by default initialized to 0.


• So, when we write static int i = 0, it’s treated exactly the same as static
int i.

✅ That’s why the variable is still stored in the BSS segment, not in the
data segment.

Now, let’s initialize the variable with a Non-Zero value:

Let’s check the size again:

Now you can clearly observe that the data segment size increased by 4
bytes while the BSS segment size decreased by 4 bytes. This change
occurred because the variable i was initialized with a non-zero value, which
means it must be stored in the initialized data segment instead of the BSS.
As a result, memory allocation shifted from BSS to the data segment.

You might also like