×

Memory layout in C

Memory layout in C

The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program.

Memory Layout In C

C program consists of:

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

Text Segment:

  • A text segment called a text/code segment is one of the sections of a program present in an object file or a memory containing executable instructions.
  • Text segments consist of binary of a compiled program. In other words, it contains machine codes of a compiled program.
  • The diagram shown above can be placed below a heap or a stack which in later stages prevents overflow of heap and stack from overwriting it.
  • Code segment consists of code that is read-only to prevent the program from modifying the instructions.
  • Text segments are sharable; hence only a single copy can be present in the memory for executed programs, like text editors, C compiler, shells, etc.
  • These are the low addresses present.

Initialized Data:

  • Data segments store program data in the form of initialized or uninitialized variables.
  • The initialized data segment, also known as the data segment, is a part of the program's virtual address space containing global, static, constant, and external (declared and defined using extern keyword) variables, which are initialized by a programmer beforehand.
  • The size of the data segment is directly proportional to the size of values in a program's source code which does not change at run time.
  • This segment of data needs to be altered at run time. Hence it has both read-only and write-only permissions. While variables initialized using constant keyword (const) will apply to read-only,

Eg:

const char* string = "Java_T_Point"  //this will be stored in a read-only area.
 

Data segment can be further grouped into:
Initialized read-only area and
Initialized read-write area.

Eg:

#include <stdio.h>
char c[] = "Java_T_Point";  // initialized global variable in read-write area
const char ch[] = "Hello"; // initialized global variable in read-only area
int main(void)
{
            static int a = 10; // static variable which is an initialized data segment
            return 0;
}

Uninitialized Data Segment:

  • In contrast to the initialized data segment, the uninitialized data segment is generally called the "bss" segment, named after an ancient assembler operator of the name "block started by symbol."
  • BSS will be included with all the uninitialized global, static, and extern (extern keyword) variables.
  • The kernel initializes an uninitialized data segment to arithmetic 0 before the compiler starts the execution. The pointer will be a null pointer; hence, it will not occupy actual space in an object file.
  • When the program is loaded, the program loader will allocate the required memory.
  • BSS starts at the end of a data segment and consists of all the global and the static variables that will be initialized to zero or will not have explicit initialization in the source code.

Eg: 

static int j;
and 
int k; //global variable

Here both will be contained in the BSS segment.

Eg:

#include <stdio.h>
char c; //uninitialized global variable stored in  bss
int main(void)
{
static int i; //uninitialized static variable stored in bss
return 0;
}

Heap:

  • Heap is a segment in which dynamic memory allocation takes place.
  • This particular area begins at the end of the BSS segment, and the addresses usually grow upward from that.
  • Heap is managed by malloc, realloc, and free, which uses "brk" and "sbrk" to adjust the size. " brk()" sets the end of a data segment by address while sbrk increments a program's data space by some increment bytes.
  • The Heap area is generally shared by most of the libraries and other dynamically loaded modules in a process.
  • It is used to allocate memory at run time.
  • The heap grows and shrinks in the opposite direction of that of a stack; they are present at the opposite ends of the process's virtual address space.
  • It is also a part of the RAM (Random Access Memory), where dynamically allocated memory is stored.

Eg:

#include <stdio.h>
int main()
{
char *p = malloc(sizeof(char)*4); //allocation of memory in heap
return 0;
}

Stack:

  • Stack is used in storing all local variables and passing arguments to the functions with the instruction's return address.
  • All local variables will be stored in the stack.
  • The stack area is traditionally adjoined to the heap area and grows in the opposite direction; when the stack meets the heap pointer, free memory will be exhausted.
  • The stack area works on the principle LIFO (Last In First Out); that is, the elements will be added to the top of the stack, and the topmost element will be popped out in the first place. It will be located in higher parts of memory. On a standard PC 8086 computer architecture, it grows towards zero; yet in some architectures, it grows in other directions.
  • The stack pointer register tracks the top of a stack. It will change the value every time a value is pushed into the stack.
  • The set of values pushed in one function is called a stack frame; it consists of return addresses.
  • Stack usually comes below the OS (Operating System) kernel, and it grows downwards to lower addresses.
  • Automatic variables will be stored along with information that will be saved each time a function is called; each time it is called, the address of where to return and the information about the environment will be saved on the stack. The new called function allocates room on the stack for its automatic and other temporary variables. This is how recursive functions work.
  • Every time a recursive function is called, a new stack frame is used, so that interference between variables does not happen.

Memory allocation is mainly used for protection. An executable file will be created and treated as a process by the OS; it should have its own address space to avoid any conflicts between data and code in a single program.


Related Topics

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.