Memory layout of Process (Running Program)
Every segment has write-protected region as well where we store constant variables. Learn the storage classes in C/C++ here.
Text/Code Segment
It is Sharable , since many process can be running the same program. So, single copy of the program is mapped into virtual address space of all of the processes.
It is part of executable file(.bin) and Virtual Address Space.
Permission : Read-only
What are all the variables it can hold ?
Global Initialized constant (may be - Purely Implementation specific)String Literals
Executable Instruction
Binary/Executable (not sure about it exactly)
Jump Tables
memory_layout.c
#include <stdio.h> int main () { return 0; }
gcc -o memory memory_layout.c
size memory
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see where the Global initialized constant variable sits in memory segment.
#include <stdio.h> int const data = 10; int main () { return 0; }
text data bss dec hex filename
1119 552 8 1679 68f memory
Data Segment
To access the variable more than one function, it has to be either in data or BSS segment.
Initialized data segment
It is portion of virtual address space and part of executable file (.bin) also.
Permission : Read-only/read-write
What are all the variables it can hold ?
Initialized Global
Initialized Static (Click Here to learn more about Static & Extern)
Initialized Extern
Initialized Volatile
memory_layout.c
#include <stdio.h> int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
The above program is base-lined. Let's add the initialized global variable.
#include <stdio.h> int data = 10; int main () { return 0; }
text data bss dec hex filename
1115 556 4 1675 68b memory
Let's see where the initialized global static variable sits in memory.
#include <stdio.h> int static data = 10; int main () { return 0; }
text data bss dec hex filename
1115 556 4 1675 68b memory
Let's see the memory segment of initialized local static variable
#include <stdio.h> int main () { int static data = 10; return 0; }
text data bss dec hex filename
1115 556 4 1675 68b memory
Let's see where the initialized extern variable sits in memory, and to understand intialized extern more here.
#include <stdio.h> int extern data = 10; int main () { return 0; }
text data bss dec hex filename
1115 556 4 1675 68b memory
Let's add the initialized volatile variable.
#include <stdio.h> volatile int data = 10; int main () { return 0; }
text data bss dec hex filename
1115 556 4 1675 68b memory
Caution: Beware of using global initialized array variables as these are going to increase your image/bin size.
Uninitialized data segment (.bss Segment)
BSS stands for Block Started by Symbol.
It is not part of Executable file. Executable merely needs to record the location and size required for the uninitialized data segment, and this space is allocated by the program loader at run time. It is initialized to 0 by kernel before program starts.
Permission : Read-only/read-write
What are all the variables it can hold ?
Uninitialized GlobalUninitialized Static
Uninitialized Extern
Uninitialized constant
Uninitialized Volatile
Local Initialized Constant variable/pointer - because the data type is constant (Interview Question)
memory_layout.c
#include <stdio.h> int main () { return 0; }
text data bss dec hex filename
1115 552 4 1675 68b memory
The above program is base-lined. Let's add the uninitialized global variable.
#include <stdio.h> int data; int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see where the uninitialized global static variable sits in memory.
#include <stdio.h> int static data; int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see the memory segment of uninitialized local static variable
#include <stdio.h> int main () { int static data; return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see where the uninitialized extern variable sits in memory, and to understand intialized extern more here.
#include <stdio.h> int extern data; int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see where the uninitialized global and local const variable sits in the memory segment.
#include <stdio.h> int const data; int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
#include <stdio.h> int main () { int const data; return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
#include <stdio.h> int main () { int const *data; return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's add the uninitialized volatile variable.
#include <stdio.h> volatile int data; int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
Let's see where the Locally initialized constant variable is stored in memory segment.
#include <stdio.h> int main () { int const data = 10; return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
To remember the difference between initialised and .bss segments is to think of "bss" as an abbreviation for "Better Save Space!".
Stack Segment
LIFO structre
In PC x86 architecture, it grows towards zero
Set of values pushed for one function call is "stack frame"
Part of executable file.
Permission : Read-only/read-write
What are all the variables it can hold ?
Recursive functionAutomatic Variables ( So called Local Variables excluding static)
Local Function's Arguments
Local Function's return Address
Local Constant variable/pointer (Uninitialized)
Memory allocation using "alloca "
memory_layout.c
#include <stdio.h> int main () { return 0; }
text data bss dec hex filename
1115 552 8 1675 68b memory
As Heap & Stack are concerned with running processes, so predicting precisely that how much certain executable takes when loaded as processes, is not possible.
Heap
Permission : Read-only/read-write
Heap is the segment where dynamic memory allocation takes place
Interview Questions
In memory, which will have the lowest address text or heap ?
Text Segment.
Why text segment is placed lower ?
To prevent heap and stack overflows from overwriting it.
Usually the instructions are placed in the text segment, and this text segment start from 0X000 address which is loaded into the CIR(instruction register).
Which are all the segments in exe file ?
Text, Data and Stack are belonging to process and are part of executable (binary) file.
What will happen if both stack and heap pointer meets ? (Interview Question)
Memory will be exhausted.
Why Text Segment is read-only ?
It prevents the data being accidentally modified via a bad-pointer value.
Why do we have the initialized and uninitialized segments separately. (Interview Question)
The initialized segment is part of executable file, But unitialized doesn't occupy any disk space in the object file. Making the uninitialized to 0 and adding into the executable file will make the exe file too large.
Which are all the segments in exe file ?
Text, Data and Stack are belonging to process and are part of executable (binary) file.
What will happen if both stack and heap pointer meets ? (Interview Question)
Memory will be exhausted.
Why Text Segment is read-only ?
It prevents the data being accidentally modified via a bad-pointer value.
Why do we have the initialized and uninitialized segments separately. (Interview Question)
The initialized segment is part of executable file, But unitialized doesn't occupy any disk space in the object file. Making the uninitialized to 0 and adding into the executable file will make the exe file too large.
Why can't we have the string literals & global uninitialised constant in data segment rather than text segment?
Text segments variables will not transferred from one function to the other whereas the data segment variables will be sent from one to the other function. Since we want to differentiate this, we will have it in a separate segment.
Where the const variable stored?
If the const variable is uninitialised, it will be stored in the BSS segment.
If the const variable is uninitialised, it will be stored in the BSS segment.
If the global const variable is initialised, it will be stored in the data or text.
If the local constant variable is initialised, it will be stored in the stack segment.
Comments
Post a Comment