C语言结构体长度的问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 20:14:35
有下面以个结构体,长度为什么是32呢?请大家帮帮忙!嘿嘿
struct s
{
int a;
char b;
double c;
union d {
char e[6];
int f[3];
}
}
sizeof(struct s)他为什么是等于32呢?32为操作系统!
一楼的怎么理解呢???疑问
a,b 对齐???

sizeof 结果是 byte 个数。
内存分配有时考虑单元对齐。
double c 最长,用8
a,b 对齐,各用 8
char e[6]; int f[3]; 共享,长度 8 足够。

4*8 = 32

个类型长度为,int-4,char-1 double-8 char e[6]-6,int f[3]-12,我检测的怎么是16呢??

struct s
{
int a;
char b;
double c;
union d {
char e[6];
int f[3];
}
}
这是一个错误的程序段。
改成这样才正确:
struct s
{
int a;
char b;
double c;
union d
{char e[6];
int f[3];
} g;
} ;

sizeof(struct s)的值为17
2+1+8+6=17