top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Why uninitialized segment of memory layout of a program known as "BSS" ?

0 votes
630 views
C: Why uninitialized segment of memory layout of a program known as "BSS" ?
posted Jul 29, 2014 by Ganesh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

The data segment has divided into two parts
1. Initialized data segment
2. Uninitialized data segment or BSS segment the BSS names after ancient assembler operator stands for "Block Started by Symbol".

The uninitialized global and static variables stored into the BSS segment which are initialized to 0 by the kernel. And initialized global and static variables are stored into the initialized data segment.

Ex.

 #include<stdio.h>
int q;
int main()
{
 printf("%d", q);
 return 0;
}

compile:

       gcc test.c
       size ./a.out

it shows:

text    data     bss     dec     hex filename
 999     252      12    1263     4ef ./a.out

Now declare one more uninitialized global variable

int p;

and now after compiling the above program, we run the same

size ./a.out

its shows

text    data     bss     dec     hex filename
999     252      16    1267     4f3 ./a.out

notice the difference between the bss partition got increased by 4 byte by adding one more uninitialized global variable.

answer Jul 29, 2014 by Prakash Singh
0 votes

The .bss segment is an optimization. The entire .bss segment is described by a single number, probably 4 bytes or 8 bytes, that gives its size in the running process, whereas the .data section is as big as the sum of sizes of the initialized variables. Thus, the .bss makes the executables smaller and quicker to load.

Some people like to remember it as 'Better Save Space.' Since the BSS segment only holds variables that don't have any value yet, it doesn't actually need to store the image of these variables. The size that BSS will require at runtime is recorded in the object file, but BSS (unlike the data segment) doesn't take up any actual space in the object file.

answer Jul 29, 2014 by Salil Agrawal
...