//https://[Log in to view URL]

//https://[Log in to view URL]

    After compiling a C program, a binary executable file(.exe) is created, and when we
execute the program, this binary file loads into RAM in an organized manner. 
After being loaded into the RAM, memory layout in C Program has six components 
which are text segment, initialized data segment, uninitialized data segment, 
command-line arguments, stack, and heap. Each of these six different segments 
stores different parts of code and have their own read, write permissions.
    If a program tries to access the value stored in any segment differently than it
is supposed to, it results in a segmentation fault error.

1. Stack
2. Heap
3. BSS (Uninitialized data segment)
4. DS (Initialized data segment)
5. Text

stack: stores local variables
heap: dynamic memory for programmer to allocate
data: stores global variables, separated into initialized and uninitialized
text: stores the code being executed

High Addresses ---> .----------------------.
                    |      Environment     |
                    |----------------------|
                    |                      |   Functions and variable are declared
                    |         STACK        |   on the stack.
base pointer ->     | - - - - - - - - - - -|
                    |           |          |
                    |           v          |
                    :                      :
                    .                      .   The stack grows down into unused space
                    .         Empty        .   while the heap grows up. 
                    .                      .
                    .                      .   (other memory maps do occur here, such 
                    .                      .    as dynamic libraries, and different memory
                    :                      :    allocate)
                    |           ^          |
                    |           |          |
 brk point ->       | - - - - - - - - - - -|   Dynamic memory is declared on the heap
                    |          HEAP        |
                    |                      |
                    |----------------------|
                    |          BSS         |   Uninitialized data (BSS)
                    |----------------------|   
                    |          Data        |   Initialized data (DS)
                    |----------------------|
                    |          Text        |   Binary code
Low Addresses ----> '----------------------'
 

Stack:

-> It located at a higher address and grows and shrinks opposite to the heap segment.

->The stack contains local variables from functions and related book-keeping data.

->A stack frame will create in the stack when a function is called.

->Each function has one stack frame.

->Stack frames contain the function’s local variables arguments and return value.

->The stack contains a LIFO structure. Function variables are pushed onto the
stack when called and functions variables are popped off the stack when return.

->SP(stack pointer) register tracks the top of the stack.

#include <stdio.h>
int main(void)
{
    int data; //local variable stored in stack
    return 0;
}
 

Heap:

->It is used to allocate the memory at run time.

->Heap area managed by the memory management functions like malloc, calloc, free,
etc which may internally use the brk and sbrk system calls to adjust its size.

->The Heap area is shared by all shared libraries and dynamically loaded modules 
in a process.

->It grows and shrinks in the opposite direction of the stack.

#include <stdio.h>
int main(void)
{
    char *pStr = malloc(sizeof(char)*4); //stored in heap
    return 0;
}
 

BSS(Uninitialized data segment):

->It contains all uninitialized global and static variables.

->All variables in this segment initialized by the zero(0) and pointer with the
null pointer.

->The program loader allocates memory for the BSS section when it loads the program.

#include <stdio.h>
int data1; // Uninitialized global variable stored in BSS
int main(void)
{
    static int data2;  // Uninitialized static variable stored in BSS
    return 0;
}
 

DS(Initialized data segment):

->It contains the explicitly initialized global and static variables.

->The size of this segment is determined by the size of the values in the program’s
source code and does not change at run time.

->It has read-write permission so the value of the variable of this segment can
be changed at run time.

This segment can be further classified into an initialized read-only area and
an initialized read-write area.

#include <stdio.h>
int data1 = 10 ; //Initialized global variable stored in DS
int main(void)
{
    static int data2 = 3;  //Initialized static variable stored in DS
    return 0;
}
 

Text:

->The text segment contains a binary of the compiled program.

->The text segment is a read-only segment that prevents a program from being
accidentally modified.

->It is sharable so that only a single copy needs to be in memory for frequently
executed programs such as text editors etc.

imp const variable

->If I have a const int which is local variable, then it is stored in the 
write protected region of stack segment.

->If I have a global that is initialised const var, then it is stored in the
data segment.

->If I have an uninitialised const var, then it is stored in the BSS segment...

->To summarize, "const" is just a data QUALIFIER, which means that first the
compiler has to decide which segment the variable has to be stored and then 
if the variable is a const, then it qualifies to be stored in the write protected
region of that particular segment.

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: