Table of contents
1.
Introduction 
2.
Diagram for memory structure of C
3.
Segments of the Memory Layout
3.1.
1. Text Segment
3.2.
2. Initialized Data Segment
3.3.
Example of Initialized Data Segment
3.4.
C
3.5.
3. Uninitialized Data Segment
3.6.
Example of Uninitialized Data Segment
3.7.
C
3.8.
4. Stack
3.9.
5. Heap
4.
Command-line arguments
5.
Examples of Command-line arguments
5.1.
1. Print the name of the program
5.2.
C
5.3.
2. Get the user's name
5.4.
C
5.5.
3. Calculate the sum of two numbers
5.6.
C
5.7.
4. Open a file and read its contents
5.8.
C
6.
Frequently Asked Questions
6.1.
What is memory layout in C?
6.2.
What are the memory models in C?
6.3.
What is memory concept in C?
7.
Conclusion
Last Updated: Aug 24, 2024
Easy

Memory layout of C program

Author Ayushi Poddar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

The memory layout of C program is divided into several segments: Text segment (stores compiled code), Data segment (stores global and static variables), BSS segment (uninitialized global and static variables), Heap (dynamically allocated memory), and Stack (stores local variables and function call information). This structure is crucial for efficient memory management.

Memory layout of  C program

Diagram for memory structure of C

diagram of memory layout of the C program

Segments of the Memory Layout

From the above figure, we can see that the C program consists of the following segments in the memory layout:

  • Stack
  • Text/Code segment
  • Initialized data segment
  • Uninitialized data segment
  • Heap

Let's discuss further regarding these segments of the memory layout.

1. Text Segment

A text segment contains code or simply a text containing executable instructions in an object file or memory. In order to prevent overflowing of stack or heap, the text section is placed below it in the memory region. Generally, this segment is sharable as only a single copy is stored in the memory for frequently executed programs such as compilers, shells, editors, etc. To prevent a program from unknowingly modifying its instructions, it is kept as read-only.

2. Initialized Data Segment

It is simply called the Data segment and is a part of the virtual address space of a program where static and global variables are initialized by a programmer. Unlike the Text segment, values of variables might need alteration during runtime, so it is not read-only. This segment is further divided into:

  • Initialized read-only area: Values of variables can not be modified here.
  • Initialized read-write area: Values of variables can be modified here.


Example of Initialized Data Segment

  • C

C

#include<stdio.h>  

char string[] = "Coding Ninjas Studio";

int main() 



  static int i = 50;   

  return 0;  

}
You can also try this code with Online C Compiler
Run Code


Explanation

The global variables here like char str[] = “Coding Ninjas Studio’ and int i=50 will be stored in the initialized read-write area in the memory. Suppose, we create the global variable like const char* string1 = "Coding Ninjas Studio"; the literal "Coding Ninjas Studio" would be stored in the initialized read area, whereas the char pointer variable would be stored in the initialized write area.

3. Uninitialized Data Segment

It is often called the “bss” segment (block started by symbol) which stores all uninitialized local, global, and external variables. If they are not initialized, then they are stored with zero value by default. This segment contains the object file where all the statically allocated variables are stored. 


Example of Uninitialized Data Segment

  • C

C

#include<stdio.h>  

char x;  // Uninitialized global variable declared 

int main() 



   static int x; // Uninitialized static variable declared 

   return 0;  

}
You can also try this code with Online C Compiler
Run Code


Explanation

Here, statically allocated objects are those without explicit initialization that is initialized with zero value. In the above code, x is an uninitialized variable, so it is stored in the uninitialized data section.

4. Stack

The stack frame is used when we define and call that function. The variables, as well as the function arguments defined in that function, are stored in the stack. This type of memory allocation is called Static memory allocation as the size of the variables is defined in the function during compile time. Whenever a function is called, a new stack frame is created, and hence stack plays an important role in the memory. It is commonly used for recursive functions. It also contains a LIFO (last in first out) structure, the program stack typically located in the higher section of memory. 

5. Heap

It is used for dynamic memory allocation. It starts at the end of the bss segment and moves upwards toward higher addresses. The functions like calloc() and malloc() allocate memory in a heap. The dynamically allocated modules and the shared memory use heap memory. To deallocate memory from the heap, the free() function is called. 

Command-line arguments

Command-line arguments are the values which are passed to a program when it is executed. They are specified after the name of the program on the command line. For example, to execute a program called MYapp with two command-line arguments, use the following command:

MYapp arg1 arg2

 

The arguments arg1 and arg2 would then be passed to the MYapp function. In C programming, command-line arguments are passed to the main() function as two variables that are argc and argv.

Examples of Command-line arguments

Here are 4 different examples of command-line arguments in C programming with their solutions:

1. Print the name of the program

  • C

C

#include <stdio.h>

int main(int argc, char *argv[]) {
printf("Program name: %s\n", argv[0]);

return 0;
}
You can also try this code with Online C Compiler
Run Code


The above code first prints the number of command-line arguments using the argc variable. Then, it prints each of the command-line arguments using the argv variable. The argv[0] variable stores the name of the program itself.

2. Get the user's name

  • C

C

#include <stdio.h>

int main(int argc, char *argv[]) {

printf("What is your name? ");
char name[100];
scanf("%s", name);

printf("Your name is: %s\n", name);

return 0;
}
You can also try this code with Online C Compiler
Run Code


The above code first prompts the user to enter their name. Then, it uses the scanf() function to read the user's input into the name variable. Finally, it prints the user's name back to the console.

3. Calculate the sum of two numbers

  • C

C

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}

char buffer[100];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}

fclose(fp);

return 0;
}
You can also try this code with Online C Compiler
Run Code


The above code first prompts the user to enter two numbers. Then, it uses the scanf() function to read the user's input into the num1 and num2 variables. Finally, it calculates the sum of the two numbers and prints it back to the console.

4. Open a file and read its contents

  • C

C

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}

char buffer[100];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}

fclose(fp);

return 0;
}
You can also try this code with Online C Compiler
Run Code


The code first opens the file specified by the first command-line argument. Then, it uses a loop to read the contents of the file one line at a time. Finally, it closes the file.

Frequently Asked Questions

What is memory layout in C?

Memory layout in C refers to the organization of different memory segments (Text, Data, BSS, Heap, Stack) during program execution.

What are the memory models in C?

Memory models in C define how memory is accessed in different environments, including models like small, medium, compact, and large, affecting code and data storage.

What is memory concept in C?

Memory in C refers to how data is stored and accessed during program execution, involving concepts like pointers, dynamic allocation, and memory segments.

Conclusion

In this article, we have discussed in detail the about memory layout of a C program and its five segments which are text/code, initialized data, uninitialized data, stack, and heap and looked at each one of them using examples. At last, discussed some of the faqs.

You can also check out our other blogs on Code360.

Live masterclass