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