简体   繁体   English

C ++中struct的对齐方式

[英]alignment of struct in C++

The scope is in x86 范围在x86中
For example, we have a struct like this: 例如,我们有一个像这样的结构:

struct X{
   int    a;   
   char   b; 
               // compiler will pad 3 bytes here
   double c;
};
X x;

I think the alignment of x should be the largest member in it, in this case is double , which is 8 bytes, 我认为x的对齐应该是其中最大的成员,在这种情况下是double ,即8字节,
so &x should be a multiple of 8 , am I right? 所以&x应该是8的倍数,对吗?

However, after some tests, my compiler(msvc 2013) says &x can also be a multiple of 4 but not 8 . 但是,经过一些测试,我的编译器(msvc 2013)说&x也可以是4的倍数而不是8

Isn't it meaning &x.c will also be a multiple of 4 ? 是不是意味着&x.c也将是4的倍数?

Where do I misunderstand? 我在哪里误会?

The C and C++ standards does not give any advice [or at least no firm rules] on what alignment should be. C和C ++标准没有给出任何对齐的建议[或至少没有确定的规则]。 It is up to each compiler (and of course, what target the compiler is for) to determine a good policy. 由每个编译器决定(当然,编译器的目标是什么)来确定一个好的策略。 It's often preferred if this policy works for the target... ;) 如果此政策适用于目标,通常是首选......;)

Since the FPU [including SSE in scalar mode] of x86 can read "double" from any byte-address and I believe there is no direct benefit in adding more than 3 bytes between the b and c elements, nor to align the whole struct to anything more than 4 bytes. 由于x86的FPU [包括标量模式下的SSE]可以从任何字节地址读取“double”,我相信在bc元素之间添加超过3个字节没有直接好处,也没有将整个结构对齐到超过4个字节的任何东西。 Doing more would waste memory. 做更多会浪费记忆力。

In some other architecture, it may well be a great benefit (or a requirement for the target to operate correctly), and it would thus align the whole struct to 8 bytes. 在一些其他架构中,它可能是一个很大的好处(或者目标正确运行的要求),因此它将整个struct对齐到8个字节。

Structure alignment in VC is a compiler option /Zp determines the byte boundaries on which your structures will be packed. VC中的结构对齐是编译器选项/ Zp确定将打包结构的字节边界。 This is normally an advanced option and not used except in unusual use cases (such as doing a memcpy of block data for transfer over the network). 这通常是一个高级选项,除了在异常使用情况下(例如通过网络传输块数据的memcpy)之外不使用。 Check the property pages of the project in order to see what the alignment is. 检查项目的属性页以查看对齐方式。

http://msdn.microsoft.com/en-us/library/xh3e3fd0.aspx http://msdn.microsoft.com/en-us/library/xh3e3fd0.aspx

Generally speaking you can't say that much about the size, the alignment or how your struct is packed, and by "generally" I mean according to the C++ standard of your choice. 一般来说,你不能说大小,对齐或你的struct如何打包,并且“通常”我的意思是根据你选择的C ++标准。

The problems are: 问题是:

  • int , char and double have no defined size according to the standard, the standard simply sets some minimum requirements , what you are going to get depends on the implementation intchardouble没有根据标准定义的大小, 标准只是设置了一些最低要求 ,你将得到什么取决于实现
  • alignment depends not only on the compiler implementation but sometimes even on the set of flags that you are passing and what hardware you are targeting, packing is extremely important for the CPU cache and some instructions may even need different padding, this also depends on what you are putting in the struct and how you are writing it. 对齐不仅取决于编译器实现,有时甚至取决于你传递的标志集和你所针对的硬件,打包对CPU缓存非常重要,有些指令甚至可能需要不同的填充,这也取决于你的内容正在放入struct以及如何编写它。

As a consequence for this kind of operations you have to literally stick with the documentation of the compiler of your choice and dive into the features that it provides you, for example here you can find an overview on how to pack data in gcc vs msvc . 作为这种操作的结果,您必须坚持使用您选择的编译器的文档并深入了解它为您提供的功能,例如,您可以在此处找到有关如何在gccmsvc打包数据的概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM