简体   繁体   English

C:未初始化的int类型大小必须大于

[英]C: Uninitialized int type size bigger then must be

Uninitialized int variable will be placed to the .bss section. 未初始化的int变量被放置到.bss部分。

But if I add such a variable - the bss section length in size 's results became bigger onto 8 bytes instead of 4 - why so? 但是如果我添加这样一个变量 - sizebss部分长度的结果变得更大到8个字节而不是4个 - 为什么呢?

Here is an example: 这是一个例子:

With one uninitialized integer: 使用一个未初始化的整数:

#include <stdio.h>

int a;
//int b;

int main () {

    printf("A size: %d\n", sizeof(a));
//    printf("B size: %d\n", sizeof(b));
    return 0;

}

The size 's result: size的结果:

 $ gcc int_szie.c -o int_szie $ size int_szie text data bss dec hex filename 1483 536 8 2027 7eb int_szie 

Uncomment b var - and now bss is 16 bytes: 取消注释b var - 现在bss是16个字节:

...
int a;
int b;

int main () {

    printf("A size: %d\n", sizeof(a));
    printf("B size: %d\n", sizeof(b));
    return 0;
...
 $ gcc int_szie.c -o int_szie $ size int_szie text data bss dec hex filename 1628 592 16 2236 8bc int_szie 

sizeof() results: sizeof()结果:

 $ ./int_szie A size: 4 B size: 4 

And I'm even afraid to ask why the data section became bigger on whole 56 bytes: 而且我甚至不敢问为什么data部分在整个56字节上变得更大:

data 536 数据536

Vs: VS:

data 592 数据592

The gcc : gcc

 $ gcc --version gcc (GCC) 7.1.1 20170630 

I ran the same code, just added long long b . 我运行了相同的代码,只是添加了long long b The code looks like this: 代码如下所示:

#include <stdio.h>

 int a;
 long long b;

 int main () {

     printf("A size: %d\n", sizeof(a));
     //    printf("B size: %d\n", sizeof(b));
         return 0;

 }

The objdump output is following: objdump输出如下:

mdeore@mdeore-Lenovo:~$ objdump -t ./a.out

./a.out:     file format elf64-x86-64

SYMBOL TABLE:
0000000000400238 l    d  .interp    0000000000000000              .interp
0000000000400254 l    d  .note.ABI-tag  0000000000000000              .note.ABI-tag
0000000000400274 l    d  .note.gnu.build-id 0000000000000000              .note.gnu.build-id
0000000000400298 l    d  .gnu.hash  0000000000000000              .gnu.hash
00000000004002b8 l    d  .dynsym    0000000000000000              .dynsym
0000000000400318 l    d  .dynstr    0000000000000000              .dynstr
0000000000400358 l    d  .gnu.version   0000000000000000              .gnu.version
0000000000400360 l    d  .gnu.version_r 0000000000000000              .gnu.version_r
0000000000400380 l    d  .rela.dyn  0000000000000000              .rela.dyn
0000000000400398 l    d  .rela.plt  0000000000000000              .rela.plt
00000000004003c8 l    d  .init  0000000000000000              .init
00000000004003f0 l    d  .plt   0000000000000000              .plt
0000000000400420 l    d  .plt.got   0000000000000000              .plt.got
0000000000400430 l    d  .text  0000000000000000              .text
00000000004005c4 l    d  .fini  0000000000000000              .fini
00000000004005d0 l    d  .rodata    0000000000000000              .rodata
00000000004005e0 l    d  .eh_frame_hdr  0000000000000000              .eh_frame_hdr
0000000000400618 l    d  .eh_frame  0000000000000000              .eh_frame
0000000000600e10 l    d  .init_array    0000000000000000              .init_array
0000000000600e18 l    d  .fini_array    0000000000000000              .fini_array
0000000000600e20 l    d  .jcr   0000000000000000              .jcr
0000000000600e28 l    d  .dynamic   0000000000000000              .dynamic
0000000000600ff8 l    d  .got   0000000000000000              .got
0000000000601000 l    d  .got.plt   0000000000000000              .got.plt


0000000000601028 l    d  .data  0000000000000000              .data
0000000000601038 l    d  .bss   0000000000000000              .bss
0000000000000000 l    d  .comment   0000000000000000              .comment
0000000000000000 l    df *ABS*  0000000000000000              crtstuff.c
0000000000600e20 l     O .jcr   0000000000000000              __JCR_LIST__
0000000000400460 l     F .text  0000000000000000              deregister_tm_clones
00000000004004a0 l     F .text  0000000000000000              register_tm_clones
00000000004004e0 l     F .text  0000000000000000              __do_global_dtors_aux


0000000000601038 l     O .bss   0000000000000001              completed.7585
0000000000600e18 l     O .fini_array    0000000000000000              __do_global_dtors_aux_fini_array_entry
0000000000400500 l     F .text  0000000000000000              frame_dummy
0000000000600e10 l     O .init_array    0000000000000000              __frame_dummy_init_array_entry
0000000000000000 l    df *ABS*  0000000000000000              abc.c
0000000000000000 l    df *ABS*  0000000000000000              crtstuff.c
0000000000400708 l     O .eh_frame  0000000000000000              __FRAME_END__
0000000000600e20 l     O .jcr   0000000000000000              __JCR_END__
0000000000000000 l    df *ABS*  0000000000000000
0000000000600e18 l       .init_array    0000000000000000              __init_array_end
0000000000600e28 l     O .dynamic   0000000000000000              _DYNAMIC
0000000000600e10 l       .init_array    0000000000000000              __init_array_start
00000000004005e0 l       .eh_frame_hdr  0000000000000000              __GNU_EH_FRAME_HDR
0000000000601000 l     O .got.plt   0000000000000000              _GLOBAL_OFFSET_TABLE_
00000000004005c0 g     F .text  0000000000000002              __libc_csu_fini
0000000000000000  w      *UND*  0000000000000000              _ITM_deregisterTMCloneTable
0000000000601028  w      .data  0000000000000000              data_start
0000000000601040 g     O .bss   0000000000000008              b       <<<<<<<< 'b' is 8 bytes
0000000000601038 g       .data  0000000000000000              _edata
00000000004005c4 g     F .fini  0000000000000000              _fini
0000000000000000       F *UND*  0000000000000000              printf@@GLIBC_2.2.5
0000000000000000       F *UND*  0000000000000000              __libc_start_main@@GLIBC_2.2.5
0000000000601028 g       .data  0000000000000000              __data_start
0000000000000000  w      *UND*  0000000000000000              __gmon_start__
0000000000601030 g     O .data  0000000000000000              .hidden __dso_handle
00000000004005d0 g     O .rodata    0000000000000004              _IO_stdin_used
0000000000400550 g     F .text  0000000000000065              __libc_csu_init
0000000000601050 g       .bss   0000000000000000              _end
0000000000400430 g     F .text  000000000000002a              _start
0000000000601048 g     O .bss   0000000000000004              a       <<<<<<<<< 'a' is 4 bytes
0000000000601038 g       .bss   0000000000000000              __bss_start
0000000000400526 g     F .text  000000000000001f              main
0000000000000000  w      *UND*  0000000000000000              _Jv_RegisterClasses
0000000000601038 g     O .data  0000000000000000              .hidden __TMC_END__
0000000000000000  w      *UND*  0000000000000000              _ITM_registerTMCloneTable
00000000004003c8 g     F .init  0000000000000000              _init

The output shows correctly, the int a is 4 bytes and long long b is 8 bytes. 输出显示正确, int a是4个字节, long long b是8个字节。

size <objfile> shows the size of .bss segment and not the size of a particular variable. size <objfile>显示.bss段的大小,而不是特定变量的大小。 To verify this fact, remove both a and b global variables from the code and now check size of .bss segment, you will still see the size as 8. 要验证这一事实,请从代码中删除ab全局变量,现在检查.bss段的大小,您仍会看到大小为8。

There is similar thread where the size differs on Windows operating system. 在Windows操作系统上有类似的大小不同的线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM