#pragma pack(n)的use(看编辑)
2016-06-21 11:12
239 查看
#include <stdio.h> #include <stdlib.h> #pragma pack(8) struct S1 { short a; long b; }; struct S2 { char c; struct S1 d; double e; }; int main(void) { struct S2 s20; printf("sizeof(struct S1) = %d\n",sizeof(struct S1)); printf("sizeof(struct S2) = %d\n",sizeof(struct S2)); printf("(int)&s20.d-(int)&s20.c = %d\n",(int)&s20.d-(int)&s20.c); printf("(int)&s20.e-(int)&s20.d = %d\n",(int)&s20.e-(int)&s20.d); printf("Hello World!\n"); return 0; }
输出:
S1
变量 类型大小 pack 起始地址 大小 空出
a 2 8 0 2[0,1]
2[2,3]
b 4 8 4 4[4,5,6,7]
S2
变量 类型大小 pack 起始地址 大小 空出
c 1 8 0 1[0]
3[1,2,3]
d 4 8 4 8[4,5,6,7,8,9,10,11]
4[12,13,14,15]
e 8 8 16 8[16,17,18,19,20,21,22,23]
结构体数据类型,就不能取其大小,而要取结构体中用过的对齐数中最大的一个,即4。
相关文章推荐
- 自定义可展开的菜单 MenuButtonLayout
- MPNowPlayingInfoCenter使用注意
- locale.Error: unsupported locale setting
- 【leetcode】112. Path Sum
- TableViewCell选中一个后另外的取消选择
- zabbix3.0于centos7搭建
- 自动化测试一
- SQL Server存储过程数组参数
- 多线程编程(四)GCD
- jquery的uploadify插件实现的批量上传V3.2.1版
- (转)simple-framework(MaliSDK框架分析)
- 提高项目5-当年第几天
- iOS 实现时间线列表效果
- 深入理解HashMap
- 百度ueditor 视频编辑问题以及图片跟视频居中报错问题解决
- c++字符串大小写转换
- 【51CTO学院三周年】从无到有的IT之旅
- SQL Server2008函数大全(完整版)
- Java网络编程
- 8-12Navigator对象