clinuxdata-segment

Strange behaviour of size utility


1st case:

#include <stdio.h>

int main(void)
{
    return 0;
}

Size output:

text       data     bss     dec     hex filename

1115        552       8    1675     68b ./a.out

2nd case:

#include <stdio.h>

int global;  // new line compared to previous case

int main(void)
{
    return 0;
}

size output:

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

Ideally it should be:

bss=12 and all other (text and data) same

3rd case:

#include <stdio.h>

int global;

int main(void)
{
    static int i;  // new line compared to previous case
    return 0;
}

size output:

text       data     bss     dec     hex filename
1115        552      16    1683     693 ./a.out

this is correct

Why the output in 2nd case is not correct?


Solution

  • You are probably compiling for a 64-bit architecture, where you get memory aligned to 8 bytes (64 bits).

    A program as simple as the one in your first case has a 4-byte starting bss, but 8 bytes is allocated for alignment purposes, so as you declared the global variable, you filled up the left 4 bytes.

    Declaring another 4-bytes variable will add 8 bytes to the bss, until it also gets filled, and so on.