【发布时间】:2013-01-10 17:27:23
【问题描述】:
#include<stdio.h>
struct test {
char a;
char b;
int c;
int d;
};
void main() {
int a,b;
char c,d;
printf("Address of a & b = %u & %u respectively\n",&a,&b);
printf("Address of c & d = %u & %u respectively\n",&c,&d);
struct test t1;
printf("The size of structure:::%d\n",sizeof(t1));
}
输出是:
Address of a & b = 3216087804 & 3216087808 respectively
Address of c & d = 3216087802 & 3216087803 respectively
The size of structure:::12
当我以这种方式声明结构时:
struct test {
char a;
int b;
int c;
char d;
};
这种情况下的输出:
The size of structure:::16
为什么当我们尝试访问内存中奇数位置的secord char变量或地址为4的倍数时不存在的变量时不会发生对齐错误?
【问题讨论】:
标签: padding