结构填充的副作用是什么 [英] What are the side effects of structure padding

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

问题描述



我知道C/C ++中的结构填充是由编译器完成的,用于数据对齐.填充会占用不必要的内存.

但是,如果我不希望编译器提供填充,那么由于填充,我可能会遇到问题.任何人都可以提供一个示例,该示例可能会由于填充而导致严重的程序错误.

提前谢谢!
Vicky

Hi,

I am aware of structure padding in C/C++ done by compiler for data alignment. Padding takes unnecessary memory.

But if I am not expecting a padding from compiler then where I can face a problem because of padding. Can anybody provide and example that can cause a serious bug in program because of padding.

Thanks in advance!
Vicky

推荐答案

只要您不对编译器生成的内容做出任何错误的假设,就可以了.

通过名称访问结构的成员不会发生任何问题,但是如果使用指针算术,可能会出现一些问题.

请注意,sizeof 将始终返回结构的实际大小,而不是构成该结构的成员的总和,因此,只要指针是的类型,使用指针移至数组中的下一项将始终是安全的.结构或步距等于结构的大小.

但是,如果您使用的是从结构开始到成员地址的偏移量,则可能会遇到问题.考虑以下结构:

Provided you don''t make any false assumptions about what the compiler is producing, you''ll be fine.

No problems will occur by accessing the members of a struct by name, but if pointer arithmetic is employed there could be some issues.

Note that sizeof will always return the actual size of the struct and not the sum of the members making up the struct, so stepping to the next item in an array using pointers will always be safe as long the pointer is of the type of the struct or if the distance stepped is equal to the sizeof of the struct.

If, however, you''re using offsets from the start of a struct to the address of a member you could run into problems. Consider the following struct:

struct my_struct
{
   char a;
   int  b;
}


这样的结构可以由编译器填充为类似的内容:


Such a struct could be padded by the compiler to something like:

struct my_struct
{
   char a;
   char padding;
   int  b;
}



因此,如果您有如下声明:



Therefore, if you have a statement like:

my_struct ms;



&ms + sizeof(char)的开头以sizeof(a)的偏移量访问b不一定会在b的开头结束.

希望这对您有帮助.



accessing b by an offset of sizeof(a) from the start of, &ms + sizeof(char), will not necessarily end up at the beginning of b.

Hope this helps.


唯一的时间结构填充(和对齐)很重要,如果您要生成的数据被传输到对填充有不同假设的对象上. >
因此,如果您具有内存映射的硬件,那么如果您天真的在硬件的基址上映射结构,尤其是当它混合使用不同大小的寄存器时,您可能会感到惊讶.

您可能会遇到的另一个问题是,通过网络将数据传输到具有不同字长或对齐要求的计算机上.例如,x86可以读取和写入任何字节地址,大多数RISC处理器不能(或不能在其默认模式下).

因此,如果您打算进行任何硬件或网络编程(包括RPC/COM/CORBA),则可能必须意识到这一问题(或在两侧使用固定大小的数据类型).对于大多数编程而言,这并不重要.

干杯,

Ash
The only time structure padding (and alignment) is important is if you''re generating data that is transferred to something that makes different assumptions about the padding.

So if you have memory mapped hardware you might get a surprise if you naively map a structure over the hardware''s base address, especially if it mixes registers of different sizes.

Another place you could get problems is transferring data over a network to a computer with a different word size or alignment requirements. For example an x86 can read and write to any byte address, most RISC processors can''t (or can''t in their default mode).

So if you intend doing any hardware or network programming (including RPC/COM/CORBA) you probably have to be aware of the issue (or use data types with fixed sizes on either side). For most programming it doesn''t really matter.

Cheers,

Ash


许多编译器将提供全局设置对齐大小或针对特定结构设置对齐大小的方法.将此设置为一个将删除该填充.请检查您的编译器文档.例如,以下是适用于GCC的文档: GCC属性文档 [ ^ ].


但是,编写依赖于这种情况的程序通常被认为是不好的形式.最好重写程序,以使其不依赖于不填充的结构,从长远来看,这将节省您的工作.

查尔斯·凯帕克斯
Many compilers will come with ways to set the alignment size either globally or for specific structures. Setting this to one will remove that padding. Check your compiler documentation for this. For example here is the appropriate documentation for GCC: GCC Attribute Documentation[^].


However, it is generally considered bad form to write a program that depends on such things. It is probably far better to rewrite your program so that is doesn''t rely on a structure not being padding, this will save you work in the long run.

Charles Keepax


这篇关于结构填充的副作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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