BSS (Block Started by Symbol) Data Segment in C language
Introduction:
BSS in Data segment is abbreviated as “Block Started by Symbol”.
The executable file of a C program is arranged within the RAM of the personal computer whenever the program is created and run.
The following figure shows the memory architecture for the C program:
The C program is divided into the following sections, as shown in the above figure:
- Text segment
- Initialized data segment
- Uninitialized data segment
- Stack
- Heap
BSS in C:
Without the explicit initializer, statically assigned objects within the C language will be initialized with a zero-value for arithmetic types either to a null pointer to serve pointer types. C implementations usually use a bit pattern made up entirely of zero-valued bits for representing zero values as well as null pointer values (although the C standard doesn't need filling bss with zero; more recent standards will need all variables in.bss to be independently initialized with a certain kind of zeroes). Because of this, the BSS segment usually contains uninitialized static local variables (local variables determined using the static keyword) along with all uninitialized objects (variables as well as constants) determined at file scope (i.e., outside any function); static local constants need to be initialized at declaration, Though they might be either explicitly or implicitly initialized with zero as their value, they usually do not belong in the BSS section since they lack a specific declaration. The BSS section may also be assigned by the implementation to statically allocated constants and variables that are initialized with the value made up entirely of zero-valued bits.
Data Section:
The data area will hold the data that we utilize in our software. Variables determined outside of the main ( ) function's scope will be saved in the data section, whereas variables defined within the main ( ) method are saved in the stack. The variables that are specified in the data section may be local or global, initialized or uninitialized, and then saved in one of these forms. As a result, the data portion is separated into four groups: local, global, uninitialized, and initialized.
Example:
#include <stdio.h>
int variable_1;
int variable_2 = 20;
void function_1 ()
{
printf (" This is function1 ");
}
int main ()
{
function_1 ();
return 0;
}
Output:
The variables variable_1 and variable_2, which are variable_2 is initialized and variable_1 is uninitialized, are defined outside of the main() operation in the program described above. Since these variables will be not a part of the main() function on the stack, they can be accessed from anywhere in the program.
- There are two parts in the data section. They are:
- Uninitialized data segment
- Initialized data segment
Uninitialized Data Segment (bss segment):
The uninitialized global, local, as well as external variables are all stored within the segment of data referred as the uninitialized data segment, or .bss segment. The default value for all global, static, as well as external variables becomes zero whenever they have not been initialized.
The bss segment is abbreviated as Block Started by symbol. The object file containing all of the arbitrarily allocated variables is contained in the bss section. Objects that be initialized with zero as their value without explicit initialization will be referred as statically allocated objects in this context. As var1 represents an uninitialized variable in the program described above, it is kept within the uninitialized data area.
Example:
#include <stdio.h>
char x; // uninitialized global variable..
int main ()
{
static int x; // uninitialized static variable..
return 0;
}
Program:
Initialized Data segment:
The term "data segment" can also refer to an initialized data segment. A data segment defines a virtual address space within the program which includes all of the programmer-initialized global as well as static variables.
Variable values within the data segment cannot be read-only; rather, they are modifiable during runtime. There are other categories into which this data segment will be divided:
Initialized read-only area: This is a region whose variable values are fixed.
Initialized read-write area: This is the region where variable values will be changed.
Example:
#include <stdio.h>
char str [] = "Welcome to javatpoint"; // global variable stored in initialized data segment in read-write area..
int main ()
{
static int k = 75; // static variable stored in initialized data segment..
return 0;
}
Output:
The initialized read-write region is where global variables like int k = 75; and char str [] = "Welcome to javatpoint" will be kept. The literal "Welcome to javatpoint" would be used to put in the initialized read area and the char pointer variables will be saved in the initialized write area if we were to establish a global variable with the syntax const char* str = "Welcome to javatpoint."
Conclusion:
In this tutorial, We detailed about BSS data segment in C language.