c – sizeof(struct)返回意外的值

c – sizeof(struct)返回意外的值,第1张

概述这应该很简单,但我不知道在哪里寻找问题: 我有一个结构: struct region{public: long long int x; long long int y; long long int width; long long int height; unsigned char scale;}; 当我做sizeof(区域),当我期待33时,它给我4 这应该很简单,但我不知道在哪里寻找问题:

我有一个结构:

struct region{public:    long long int x;    long long int y;    long long int wIDth;    long long int height;    unsigned char scale;};

当我做sizeof(区域),当我期待33时,它给我40.

有任何想法吗?

(mingw gcc,win x64 os)

解决方法 它将结构填充为8字节边界.所以它实际上是在内存中占用40个字节 – sizeof返回正确的值.

如果您只想要33个字节,则指定打包属性:

struct region{public:    long long int x;    long long int y;    long long int wIDth;    long long int height;    unsigned char scale;} __attribute__ ((packed));
总结

以上是内存溢出为你收集整理的c – sizeof(struct)返回意外的值全部内容,希望文章能够帮你解决c – sizeof(struct)返回意外的值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1233837.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存