#include "stdio.h"
struct T
{
int a;
union D
{
int b;
double c;
}pt;
T *d[2];
}str;
int main()
{
printf("%d\n",sizeof(str));//结果为24
printf("%d\n",sizeof(str.pt));//结果为8
printf("%d\n",sizeof(str.d));//结果为8
return 0;
}
上述由于内存对其可以理解,而下面就让我迷糊了
#include "stdio.h"
struct T
{
int a;
union D
{
int b;
double c;
}pt;
T *d[2];
}str;
int main()
{
printf("%d\n",sizeof(str));//结果为24,
printf("%d\n",sizeof(str.d));//结果为8
return 0;
}
内存对齐理解。。
而下面却迷惑不解,为什么呢?
#include "stdio.h"
struct T
{
int a;
union D
{
int b;
double c;
};//此处去掉union变量pt,
T *d[2];
}str;
int main()
{
printf("%d\n",sizeof(str));//结果为12,迷惑不解,为什么呢?
printf("%d\n",sizeof(str.d));//结果为8
return 0;
}
第二个程序代码如果内存对齐的话不应该是12啊?
为什么会有这种情况产生呢?
请教。。