C语言结构体字节对齐
来源:互联网 发布:戴墨镜抽烟软件 编辑:程序博客网 时间:2024/05/19 17:07
结构体对齐方式有两种:
一:系统默认方式对齐
结构体的大小是结构体成员中占用字节最大的类型的整数倍。
struct A
{
char ch;
short sh;
int it;
double d;
};
sizeof(struct A) = 16; // sizeof(double)*2
struct B
{
double d;
int it;
short sh;
char ch;
};
sizeof(struct B) = 16; // sizeof(double)*2
struct C
{
char ch;
short sh;
double d;
int it;
};
sizeof(struct C) = 24; // sizeof(double)*3
struct D
{
int it;
double d;
char ch;
short sh;
};
sizeof(struct D) = 24; // sizeof(double)*3
struct E
{
struct F
{
int ia;
char ca;
}ff;
char ch;
};
sizeof(struct E) = 12; // struct F中最大数据类型int的字节整数倍。
接下来看看每个结构体的具体地址分配:
二:#pragma pack(int)指定
#pragma pack(2)
sizeof(struct A) = 16;
sizeof(struct B) = 16;
sizeof(struct C) = 16;
sizeof(struct D) = 16;
sizeof(struct E) = 8;
最小字节分配为2。
#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue))
#define OFFSET(struct,member) ((char *)&(((struct *)0)->member) - (char *)0) // 查看结构体成员偏移量
PRINT_D(OFFSET(struct A,ch)); // 结构体A的ch成员的偏移量
推荐:http://www.cnblogs.com/motadou/archive/2009/01/17/1558438.html
- C语言结构体的字节对齐
- C语言中结构体字节对齐
- C语言结构体 字节对齐
- C语言结构体的字节对齐
- C语言中的结构体字节对齐
- C语言结构体字节对齐
- C语言结构体的字节对齐
- C语言结构体的字节对齐
- C语言结构体字节对齐总结
- C语言结构体字节对齐
- C语言结构体字节对齐
- c语言结构体字节对齐问题
- C语言结构体字节对齐
- c结构体字节对齐
- C语言结构体的字节对齐原则
- C语言结构体的字节对齐原则
- C语言结构体的字节对齐原则
- 关于c语言字节对齐(结构体)
- 集合(二)List
- linux strace追踪用法
- 百练2967:特殊日历计算题解
- C++ 中的类型限定符
- 一些好用的shell命令
- C语言结构体字节对齐
- rac客户端负载均衡测试
- 如何编译运行tensorflow的demo
- 数据类型和表达式
- 【题解】余数之和 数论 分块优化 [CQOI2007] BZOJ – 1257
- [模板]
- angular2部署到tomcat中,让他跑起来
- 欢迎使用CSDN-markdown编辑器
- day01-java开发前奏