Why bss value shown by size command doesn't increase even after adding uninitialized global variable?

509 views Asked by At

I am using ubuntu 64 bit and I am trying to find the size of .bss segment for various variables as below.

first program:

#include <stdio.h>
main()
{
}

size command is giving below result for the executable generated by above program.

text       data     bss     dec     hex filename
1115        552       8    1675     68b a.out

second program:

#include <stdio.h>
int a = 10;
main()
{
}

size command is giving below result for the executable generated by above program.

text       data     bss     dec     hex filename
1115        556       4    1675     68b a.out

third program:

#include <stdio.h>
int a;
main()
{
}

size command is giving below result for the executable generated by above program.

text       data     bss     dec     hex filename
1115        552       8    1675     68b a.out

Note:

I haven't used any flags while compiling.

Could some one please clairfy below questions?

1) Why `bss` is 8 bytes for first program even though there are no uninitialized global or static variables?
2) Why `bss` is reduced to 4 bytes when I added initialized global variable in second program?
3) Why `bss` is showing 8 bytes for the third program?
4) What are dec and hex in the output given by size command?
1

There are 1 answers

3
SnickySnacks On BEST ANSWER

this is your coworker. You left before I could tell you your answers.

1) Why bss is 8 bytes for first program even though there are no uninitialized global or static variables?

I don't have a clear answer for this. I suspect the compiler is using 4 bytes for something and because the data segment is already 69*8 = 552, it has to allocate a full 8 bytes for the bss.

2) Why bss is reduced to 4 bytes when I added initialized global variable in second program?

data + bss will always be divisible by 8. Because your program only needs 4 bytes for the variable, the compiler takes the unused bytes from bss into the data segment.

3) Why bss is showing 8 bytes for the third program?

This goes back to 1 and 2, when you aren't initializing your variable it is getting put in bss. bss at this point has 2 ("a" + an internal compiler variable) variables in it. You can test this by adding additional uninitialized variables. Every other variable you add (starting with the first, if bss was 4) will increase bss by 8.

4) What are dec and hex in the output given by size command?

data + text + bss = dec, same value in hex

Hope this helps. Swing by my desk if you'd like to know more.