结构体对齐
来源:互联网 发布:法学网络课程 编辑:程序博客网 时间:2024/05/26 08:42
pragma pack(1) nor __attribute__ ((aligned (1))) works
My code used to work in the past, but now the struct size suddenly is 16 bytes. It used to be 13 bytes. I recently upgraded from Xcode 4.2 to Xcode 4.3.1 (4E1019).
#pragma pack(1)struct ChunkStruct { uint32_t width; uint32_t height; uint8_t bit_depth; uint8_t color_type; uint8_t compression; uint8_t filter; uint8_t interlace;};#pragma pack()STATIC_ASSERT(expected_13bytes, sizeof(struct ChunkStruct) == 13);
I have tried unsuccesfully using
#pragma pack(push, 1)/* struct ChunkStruct { ... }; */#pragma pack(pop)
I have also tried the following, but no luck
struct ChunkStruct { uint32_t width; uint32_t height; uint8_t bit_depth; uint8_t color_type; uint8_t compression; uint8_t filter; uint8_t interlace;} __attribute__ ((aligned (1)));
How to pack structs with Xcode 4.3.1 ?
1 Answer
Xcode uses the gcc
and clang
compilers which both use __attribute__((packed))
to designate struct packing.
struct foo { uint8_t bar; uint8_t baz;} __attribute__((packed));
Using __attribute__((aligned(1)))
tells the compiler to begin each struct element on the next byte boundary but doesn't tell it how much space it can put at the end. This means that the compiler is allowed to round the struct
up to a multiple of the machine word size for better use in arrays and similar. __attribute__((packed))
tells the compiler to not use any padding at all, even at the end of the struct
.
- 结构体对齐(内存对齐)
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 内存对齐 结构体对齐
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 内存对齐.结构体对齐
- 结构体对齐原理
- 结构体字节对齐
- 结构体对齐
- 结构体 对齐
- 结构体对齐
- 结构体对齐
- 结构体字节对齐
- 结构体对齐
- 使用JMeter进行基本压力测试
- Zend Studio 配色方案插件
- string.IsNullOrEmpty()是什么意思啊
- bzoj1048 分割矩阵 记忆化搜索
- Week4-4Earley Parser
- 结构体对齐
- Android NDK学习之 一. Android NDK简介
- UIImageView的使用
- c语言内存分配
- 在Spring3中,配置DataSource的方法有6种。
- CentOS 7中没有ifconfig命令,而且不能发现eth0
- 趣味i**1/((i-1)**(i-1)) 发现
- 正则表达式判断用户昵称
- (as)Android SDK在线更新镜像服务器