这是有可能告诉GCC,它不应该使用填充结构。这是使用__attribute__((packed))完成的。
typedef struct {
uint8_t startSymbol;
uint8_t packetType;
uint32_t deviceId;
uint16_t packetCRC;
} PacketData __attribute__((packed));但是,最新的Xcode使用LLVM,不识别该属性。如何定义LLVM的打包结构?
这个问题的全部描述可以找到here。
更新我使用Xcode 4.5.1用于iOS,它使用AppleLLVM4.1编译器。在上面的代码示例中,我在Xcode中得到了“packed”属性被忽略的警告。
发布于 2012-12-03 17:38:30
你真的试过吗?我只是在我的机器上测试了它,__attribute__((packed))使用clang编译得很好。
编辑:I得到了相同的警告(“警告:未使用的填充属性”)
typedef struct {
int a;
char c;
} mystruct __attribute__((packed));在这种情况下,sizeof(mystruct)是8。
然而,
typedef struct __attribute__((packed)) {
int a;
char c;
} mystruct;工作得很好,sizeof(mystruct) 5岁。
结论:为了达到这一目的,该属性似乎需要先于结构标签。
发布于 2014-08-27 16:58:00
您可以使用预处理器指令为结构指定字节对齐,这样编译器就不会进行填充:
#pragma pack(1)
typedef struct {
char t1;
long long t2;
char t3;
} struct_size_test;
#pragma options align=reset请参阅关于堆栈溢出的this问题的答案。
https://stackoverflow.com/questions/13688625
复制相似问题