微软官方文档说#pragma pack 指令的作用是为结构、联合和类成员指定 pack 对齐。的主要作用就是改变编译器的内存对齐方式,这个指令在网络报文的处理中有着重要的作用,#pragma pack(n)是他最基本的用法,其作用是改变编译器的对齐方式, 不使用这条指令的情况下,编译器默认采取#pragma pack(8)也就是8字节的默认对齐方式,n值可以取(1, 2, 4, 8, 16) 中任意一值。
来写一个程序试下:
#include <iostream>
#include <stddef.h>
using namespace std;

struct Test{
short a;//2
int b;//4
double c;//8
long d;//8
};

#pragma pack(2)
struct S {
short a;//2
int b;//4
double c;//8
long d;//8
};
int main()
{
cout << offsetof(Test, a) << endl;
cout << offsetof(Test, b) << endl;
cout << offsetof(Test, c) << endl;
cout << offsetof(Test, d) << endl;
cout << "---------------" << endl;
cout << offsetof(S, a) << endl;
cout << offsetof(S, b) << endl;
cout << offsetof(S, c) << endl;
cout << offsetof(S, d) << endl;
system("pause");
return 0;
}
该程序在win32位PC机下打印结果如下:
0
4
8
16
---------------
0
2
6
14

最新文章

  1. jQuery-表单流程导航
  2. Python全栈开发day5
  3. eclipse 和 android studio 编译时报 Unsupported major.minor version 52.0错解决办法
  4. Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs 前端主体结构(五)
  5. 解决 internet connection sharing 启动不了
  6. SQL Server系统表sysobjects介绍与使用
  7. 分布式内存对象缓存系统Memcached-概述
  8. 同时安装Xcode6和Xcode7导致出现N多UUID 模拟器解决办法
  9. C#实现http断点下载
  10. MySQL批量导出以某数字或字母开头的表
  11. Linux下打造全方位立体监控系统
  12. js 循环 常用方法
  13. iOS.Animations.by.Tutorials.v2.0汉化(二)
  14. 《java入门第一季》之java语法部分小案例
  15. KNN算法思想与实现
  16. turnserver 配置说明记录
  17. like contains
  18. loadrunner&#160;场景设计-制定负载测试计划
  19. BZOJ 4804: 欧拉心算
  20. yum 安装 Mysql 5.7,忘记密码解决方案

热门文章

  1. LibSVM学习详细说明
  2. 【转】IntelliJ IDEA搭建Spring环境
  3. qq截图原理
  4. 【转】PL/SQL 使用技巧
  5. API网关——Kong实践分享
  6. 【WIP】swift3的timer的用法
  7. (水题)洛谷 - P1308 - 统计单词数
  8. 704. Binary Search
  9. ssh密钥分发之二:使用sshpass配合ssh-kopy-id编写脚本批量分发密钥:
  10. Qt容器类之三:通用算法