帮看看内存对齐问题。。

井白人 井白人 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#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啊?
为什么会有这种情况产生呢?
请教。。
...全文
评论 5
收藏 已收藏
分享
返回顶部
井白人
C++ 语言 65,214 成员 250,514 内容