【发布时间】:2018-02-19 06:30:47
【问题描述】:
我正在使用
processor architecture: x86_64
OS : Debian GNU/Linux 9 (stretch) 64-bit
GCC compiler ver. : 6.3.0
如果我编译这段代码 -
struct test {char a; int b;}
test;
printf("%ld", sizeof(test);
然后输出是 - 8 我假设它是因为 4 个字节填充,我的意思是 1+3+4 现在我尝试了这段代码-
struct test{char a; double b;}
test;
这给了我 16 个字节,然后我想,可能是因为 8 个字节的填充,即 - 现在我尝试这段代码时是 1+7+8 -
struct test{char a; long double b; char c;}
test;
这给了我 48 个字节
在我使用 gcc 的系统中,int = 4 byte,double = 8 byte,char = 1 byte,long double = 16 byte。
我的问题是这是如何工作的?为什么不是统一的填充?
【问题讨论】:
-
“对齐”是您应该研究的关键字。
-
这一行
printf("%ld", sizeof(test);缺少括号。请发布显示问题的Minimal, Complete, and Verifiable example。无论如何应该是printf("%zu", sizeof(test));