#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
メモリ内の奇数の場所にある 2 番目の char 変数、または 4 の倍数のアドレスに存在しない変数にアクセスしようとすると、アラインメント違反が発生しないのはなぜですか?