Why the int type takes up 8 bytes in BSS section but 4 bytes in DATA section

前端 未结 1 1737
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 05:53

I am trying to learn the structure of executable files of C program. My environment is GCC and 64bit Intel processor.

Consider the following C code a.cc

相关标签:
1条回答
  • 2020-12-11 06:21

    It doesn't, it takes up 4 bytes regardless of which segment it's in. You can use the nm tool (from the GNU binutils package) with the -S argument to get the names and sizes of all of the symbols in the object file. You're likely seeing secondary affects of the compiler including or not including certain other symbols for whatever reasons.

    For example:

    $ cat a1.c
    int x;
    $ cat a2.c
    int x = 1;
    $ gcc -c a1.c a2.c
    $ nm -S a1.o a2.o
    
    a1.o:
    0000000000000004 0000000000000004 C x
    
    a2.o:
    0000000000000000 0000000000000004 D x
    

    One object file has a 4-byte object named x in the uninitialized data segment (C), while the other object file has a 4-byte object named x in the initialized data segment (D).

    0 讨论(0)
提交回复
热议问题