本地禁用填充 [英] Locally disable padding

查看:92
本文介绍了本地禁用填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为某些数据结构编写了一个解析器,经过几个小时的调试,我发现问题是Visual Studio不解释结构,我告诉它。它似乎使用了一些填充

I write a parser for some data structure, after hours of debugging I found out that the problem is Visual Studio doesn't interpret the structures as I tell it. It seems some "padding" is used

struct foo { 
unsigned char a; //0x00
unsigned char b; //0x01
unsigned int c; //0x02
unsigned int d; //0x06
unsigned int e; //0x0A
unsigned int f; //0x0E
//0x12
};



我希望sizeof(foo)= 4 * 4 + 2 = 18 (foo)= 20。有没有任何可能性,只为这个特殊的结构打开填充关闭?我尝试了

I expected "sizeof(foo)=4*4+2=18" but I get "sizeof(foo)=20". Is there any possibility to turn padding off just for this special struct? I tried

__declspec(align(1)) struct foo { ...

但它不工作。感谢您的帮助。

but it does not work. Thank you for your help.

推荐答案

使用 #pragma pack 为:

#pragma pack(push, 1)
struct foo { 
  // etc..
};
#pragma pack(pop)

这篇关于本地禁用填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆