Memory Layout of C For Beginners

vikas v
4 min readJan 27, 2019

--

simple illustration for memory layout

C language is designed in such a manner so that the programmer can decide the amount of memory he/she shall use for the program. When we runs a program, the executable image is loaded into RAM of in an organized manner. Above diagram shows how the memory is wisely used.

1. Text or Code Segment

When we compile a program a binary gets generated. Suppose we generate a.out for a hello world c program. a.out consists of the instructions and these instructions are stored in the text segment. Text segment is read only and shared memory.

For example : we can open multiple sessions of a single application because the binary of that application consists of instructions and these instructions are stored in text segment.

2. Initialized Data Segment

Initialized data segment or simply the Data Segment stores all global, static, constant and external variables that are initialized beforehand. Data segment is not read only since it can be changed during run time. Data segment can further be classified as read only area and read write area. The variable initialized as const will come under read only area. Remaining all will come under read write area.

For ex : const char* string = “memory layout” // stored in read only area

#include <stdio.h>char c[]="memory layout";     /*  global variable stored in Initialized Data Segment in read-write area*/
const char s[]="jaangoz hub"; /* global variable stored in Initialized Data Segment in read-only area*/
int main()
{
static int i=11; /* static variable stored in Initialized Data Segment*/
return 0;
}

3. Uninitialized Data Segment

Uninitialized data segment is also known as bss(block started by symbol) segment. Every member of this segment is initialized by the kernel to arithmetic 0 before the program starts executing.

Uninitialized data starts at the end of data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.

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

4. Heap

Dynamic memory allocation takes place in heap. Stack segment mostly begins at the end of the BSS segment and grows upwards to higher memory addresses. Using malloc and calloc we can allocate memory in heap. The heap area is shared by all shared libraries and dynamically loaded modules in a process.

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

5. Stack

Stack comes below OS kernel space and opposite to heap. Stack grows downwards to lower address but it depends on the architecture. Stack is of Last in First Out(LIFO) structure.

All local variables are stored in stack segment. Also stack segment is used for passing arguments to the functions along with the return address of the instruction which is used to be executed after the function call is over. Local variables have a scope to the block which they are defined in, they are created when control enters into the block.

Automatic variables are stored along with the information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.

6. Command Line Arguments

Command line arguments like argc and argv, and environment variables are stored in this memory.

Thanks,

--

--

vikas v

#wlan_device_driver_developer #travelenthusiastic