gpt4 book ai didi

c++ - 在 GCC 中为类使用属性打包

转载 作者:行者123 更新时间:2023-11-30 03:22:55 25 4
gpt4 key购买 nike

海湾合作委员会文件 says :

You may only specify the packed attribute attribute on the definitionof an enum, struct or union, not on a typedef that does not alsodefine the enumerated type, structure or union.

这是否意味着我不能将此属性应用于类?

最佳答案

我在 GCC 文档中找不到清楚的答案,但通过以下实验,似乎可以。

#include <iostream>


struct UnPackedStruct {
unsigned char a;
int b;
};

struct __attribute__ ((__packed__)) PackedStruct {
unsigned char a;
int b;
};

class __attribute__ ((__packed__)) PackedClass{
unsigned char a;
int b;
};

int main()
{
std::cerr << "sizeof( UnPackedStruct ): " << sizeof(UnPackedStruct)
<< ", sizeof( PackedStruct ): " << sizeof(PackedStruct)
<< ", sizeof( PackedClass): " << sizeof(PackedClass)
<< "\n";

return 0;
}

输出:

sizeof( UnPackedStruct ): 8, sizeof( PackedStruct ): 5, sizeof( PackedClass): 5

Try it with online compiler

关于c++ - 在 GCC 中为类使用属性打包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com