MEMORY MAPPING IN C
INTRODUCTION
These components make up an ordinary storage visualization of a C program.
- Text segment
- Initialized data segment
- Uninitialized data segment
- Stack
- Heap
The diagram explains the memory mapping in c.
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c1.png)
Let us discuss every component in detail.
1.Text segment:
The software stored within a data format or within storage that includes operational commands can be referred to as a written category, occasionally referred to as a plain portion of code, or just as language.
A written section might be utilized as a data section located beneath its heap or stacks to avoid heap overflow and stack overflow from replacing them.
With regularly used applications like editors for text, the C compiler, shells, and so forth, just one copy of the portion of data i commonly require b is kept in the cache. Additionally, the written section is frequently only for reading, to prevent the software from inadvertently and unintentionally changing its directives.
2.Initialized data segment:
Typically referred to as the information segmentation, the data section is a section of an initiative's digital address space that houses general factors, including persistent variables that the developer has defined.
Because the contents of the parameters can be changed during execution, it should be understood that the information section remains for reading only. Established only for the reading region, along with writing zones, constitute additional categories for this part.
For example, a C line such as int debug=1 beyond the main file (i.e., global variable) with an external string that uses char s[] = "hello world" might be put on the loaded readable sector.
For instance, the information segments contain static int i = 10 and the global value int i = 10.
The program for initialized data segment in C.
*A constant that is initialized data segment.
const hello* = "Data segment";
Example for initialized data segment:
#include<stdio.h>
int gpar= 10;
char par1[25] = "Initilized data segment";
/* Global parameters are kept within the generated information segments only for the reading section.*/
const int gpar2 = 56;
int main() {
// within an information segment with an assigned dynamic parameter.
static int s = 15;
printf("The variable s is:%d",s);
return 0;
}
OUTPUT:
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c2.png)
Description of the program:
Global parameters gpar and par1 have been saved within the reading and writing portions that comprise the initialized data segment of the above instance, since they have been set beyond the bounds of the primary () function. In contrast, the global parameter gpar1 was created using the term const and consequently resides in the only reading portion of an initialized data segment. This section of the RAM additionally houses fixed parameters, such as s.
3.Uninitialized data segment:
The uninitialized information category, commonly referred by the acronym "bss". Initially went by this, since it was a vintage assembly operation, which meant "block started by symbol." When the application begins running, the operating system initializes the information in that section to a mathematical 0.
Each global factor, including static parameters set to zero and lacking a clear introduction within the original code, will appear in the uninitialized information, which begins near the conclusion of the information section.
As an example, the BSS element might have been a parameter with the declaration of static int i;
Examples include the BSS element, which might involve a global field defined in j.
*A brief explanation is as another name given to an uninitialized data segment was BSS. Once the application packs, space is set aside to store this portion. The kernel initializes every value in BSS as a mathematical 0 and all references to a null pointer before the C code runs. A numeric zero is used to initialize fixed and universal parameters in BSS. The information in this section requires read-write rights since parameters saved in the BSS may alter their contents.
Example for bss section:
*An example of an uninitialized data segment is as follows.
#include <stdio.h>
// bss section contains a global parameter that still needs to be created.
int glopar;
int main()
{
// Fixed parameter not introduced and kept in bss.
static int stapar;
printf("The global variable is= %d\n", glopar);
printf("The static variable is= %d\n", stapar);
return 0;
}
OUTPUT:
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c3.png)
Description of the program:
The parameters global and stapar have not been defined in this instance. As a result, these remain within the BSS sector of the C storage architecture. The kernel initializes these fields using their zero value before the program's processing starts. Displaying a variable's values as they appear in the application allows you to confirm them.
4.Stack:
Historically, the stack's region became next after the heap region and expanded reversely; the available memory became scarce whenever a stack pointer and the heap reference crossed. (These individuals can be put almost everywhere. Thanks to today's vast addresses and digital memory approaches, yet they usually develop in opposing directions.)
The program's system, a LIFO architecture typically found in the upper regions of memories, is contained in the stacking region. It expands towards location zero according to the typical PC x86 machine architecture, although it can expand in the reverse direction on multiple additional systems.
Whenever an integer gets "pushed" into a stack, a "stack pointer" is signed up, maintaining a stack's surface record. The "stack frames" comprise the data collection delivered during one function operation; a stack frame has to contain an exit location.
Stack, which holds data maintained whenever the function is identified, and automated parameters.
The location of when to go back and other environment-specific data, including a few hardware records, remain in the stack of data every time an operation is invoked. Next, space is allocated in the stack to accommodate the freshly launched function's regular and temporary parameters.
A brief explanation of stacks is that, the stacked section extends downward toward the lowest location that adheres to the so-called LIFO (Last In, First Out) framework, but utilizes the system's platform. Stacks develop precisely in a different manner from heaps.
In addition to specific other data, such as the location of each command that will be performed following the function's call. The portion of the stack also keeps the contents of local parameters and variables provided by the method.
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c4.png)
Example for Stack memory allocation:
#include<stdio.h>
void func() {
// stack of locally saved values
// immediately after the function's invocation.
int b, s;
printf("The variable is=%d",b);
}
int main() {
// stack of locally saved parameters
int var= 5;
char value[26];
func();
return 0;
}
OUTPUT:
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c5.png)
Since parameters in this case, are defined within the bounds of the underlying operation, they are each kept in a stacked storage structure using C.
They occupy space until their intended use is carried out.
Description of the program:
the initial main() in the source code above begins running, or the stack frame with main() is created and placed down the application's stack, containing information about value and var variables. After calling func within the central, additional stack frames containing variables b and s values are created and sent into them. Once the application is finished, a main stack frame has also been pushed off following the running func, and the corresponding variable is not allocated to anything.
5.Heap:
Flexibly allocated memory often occurs in the heaps' section.
The final address of a BSS section indicates the very start of the heap's location, which expands to greater values after that. It should be noted that the employing of brk/sbrk and one "heap area" isn't necessary to carry out the agreement of malloc/realloc/free; their bodies could be put into effect by employing a map to allocate possibly non-contiguous areas of digital memory within the process of addressing space. Malloc, Realloc and Free are liable for managing a Heap surface. They can utilize the brk as well as sbrk system commands to modify the size of it. Both communal libraries, including conditionally imported components, cooperate in the Heap space.
- A brief explanation for the heap is a term employed to describe storage randomly allocated throughout execution.
- In most cases, the heap starts after each Bs section and grows and contracts in the same manner as the stack.
- Reservations within the heap's sector are managed using malloc, calloc, free, realloc, etc. Within a company, the sbrk and brk calls to the system are employed to modify memory utilization inside the heap sector.
- A procedure's library resources and dynamically loaded components share a heap information section.
Example for memory allocation to heap:
#include <stdio.h>
#include <stdlib.h>
int main() {
// memory allocated in heap segment
char *var = (char*) malloc ( sizeof(char) );
printf("The memory is allocated to heap segment");
// ..
return 0;
}
OUTPUT:
![MEMORY MAPPING IN C](https://static.tutorialandexample.com/c/memory-mapping-in-c6.png)