Strange size utility behavior

1st case:

#include <stdio.h>

int main(void)
{
    return 0;
}

Output Size:

text       data     bss     dec     hex filename

1115        552       8    1675     68b ./a.out

Second case:

#include <stdio.h>

int global;  // new line compared to previous case

int main(void)
{
    return 0;
}

the size:

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

Ideally, this should be:

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

The third case:

#include <stdio.h>

int global;

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

the size:

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

it is right

Why is the conclusion in the second case incorrect?

+4
source share
1 answer

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

The program, as simple as in the first case, has a 4-byte start bss, but 8 bytes are allocated for alignment, since you declared a global variable, you filled in the left 4 bytes.

Declaring another variable of 4 bytes will add 8 bytes to bss until it is full, etc.

+2
source

All Articles