Understanding the memory layout of c programs

Vamsi KrishnaVamsi Krishna
4 min read

When you write a C program, you need to understand how memory is allocated and managed in your program. The memory layout of a C program is a crucial concept to understand to write efficient and bug-free code.

In this article, we'll explore the different memory segments that make up the memory layout of a C program.

By the end of this article, you should have a better understanding of how C programs allocate and manage memory, which will help you write more efficient and optimized code. So, let's dive into the memory layout of a c program

Memory assigned to a c program in a typical architecture can be broken down into 4 segments:

1. Code (text)

2. Global / static variables

3. Stack

4.Heap

There are four main memory segments in a C program: text, data, bss, and stack. The text segment stores the program's executable code, while the data segment contains global and static variables that are initialized before the program starts running. The bss segment stores uninitialized global and static variables, and the stack segment stores local variables and function call information.

Let's take a closer look at each segment:

Text Segment

The text segment, also known as the code segment, stores the program's executable code. This segment is typically read-only, which means that the code cannot be modified during program execution. It includes instructions for the processor to execute, such as arithmetic operations, conditional statements, and loops.

Global/static variable

For the unversed, The lifetime of global and static variables are till the end of the program and as the name suggests the scope of global variable is global I.e it can be accessed throughout the entire program whereas scope of static variable is local I.e it can be accessed within the block it is defined.

The section is further divided into two segments: Data segment and Bss segment.

Data Segment

The data segment contains global and static variables that are initialized before the program starts running. This includes variables that are explicitly initialized, such as integer and character variables, as well as initialized arrays and pointers.

For example, in the below code, integer a and char v are initialized global variables and integer b is a initialized static variable.

#include <stdio.h>

// global variables:
int a = 10;
char v = "code";

int main ()
{
    int static b = 25; //static variable

    return 0;
}

BSS Segment

The BSS segment stands for "Block Started by Symbol." It contains uninitialized global and static variables that are set to zero or NULL when the program starts running. This segment is used to save memory space by not allocating memory for uninitialized variables until they are actually used.

for example, in the below code, global variable aand static variable b are uninitialized.

#include <stdio.h>

int a;

int main ()
{
    int static b;

    return 0;
}

Stack Segment

The stack segment stores local variables and function call information. This segment grows downward, and it's used to keep track of function calls, local variables, and program execution. When a function is called, its parameters and return address are pushed onto the stack. When the function returns, the values are popped off the stack, it works similarly to the Stack data structure as the stack segment follows the Last in first out principle.

#include <stdio.h>

int sum;

int main () 
{
    int a = 10, b = 25;
    printf("Sum = %d\n",sum(a, b));

    return 0;
}
int sum() 
{
return a+b;

Heap segment

the heap memory segment in C is a region of memory that is used for dynamic memory allocation. It allows you to allocate memory for variables or data structures of unknown or variable size at runtime.

To allocate memory on the heap in C, you can use the malloc() function. The malloc() function takes a single argument, which is the number of bytes to allocate on the heap. It returns a pointer to the beginning of the allocated memory block.

For example, if you want to allocate memory for an array of integers, you can use the following code:

int *array;
array = (int*) malloc(10 * sizeof(int));

This code allocates enough memory for an array of 10 integers and assigns the pointer to the beginning of the allocated block to the variable array

Once you have allocated memory on the heap, you must remember to free it when you are finished using it. This is important because if you don't free the memory, it will remain allocated and unavailable for other parts of your program to use.

To free memory on the heap in C, you can use the free() function.

free(array);

Conclusion

In conclusion, understanding the memory layout of a C program is important for writing efficient and optimized code. By knowing how memory is organized, you can allocate and manage memory in a way that reduces memory usage and improves performance. So, next time you write a C program, keep in mind the four main memory segments: text, global / static data, stack and heap.

11
Subscribe to my newsletter

Read articles from Vamsi Krishna directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vamsi Krishna
Vamsi Krishna

Hi there! I am Vamsi, a passionate programmer who loves to contribute to open source. I'm pursuing Btech in CSE Currently exploring and learning things mostly related to tech. I'm working on building my logical & problem-solving skills. I love blockchain and web 3, Interested in Web & Android development.