字符数组的对齐 [英] Alignment of char arrays

查看:289
本文介绍了字符数组的对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何STL向量通常执行?它有一个原始存储的char []中,它偶尔会调整大小按一定的系数,然后调用新的位置时,一个元素是pushed_back(一个很有趣的语法形式,我应该注意 - 语言学家应该研究这些动词形式pushed_back :)
再就是对齐要求。因此,一个自然的问题是我怎么能叫一个新的位置上一个char [],并确保对齐要求得到满足。所以,我找遍2003的C ++标准的单词对齐,并找到了这些:

第3.9条第5

对象类型有调整的要求(3.9.1,3.9.2)。一个完整的对象类型的排列是再$ P $实现定义整数psenting的字节数;一个对象被分配在符合其对象类型的对准要求的地址。

第5.3.4条10:

新-EX pression通过空间要求分配函数类型性病的第一个参数::为size_t量。这样的说法应不小于所创建的对象的大小更小;比正在创建的对象的大小仅当对象是一个数组它可能更大。对于char和unsigned char型数组,新-EX pression结果和分配函数的返回地址之间的差额是任何对象类型的最严格对齐要求(3.9),其大小的整数倍不大于所创建的阵列的大小。 [注:由于分配的功能被假定为指针返回到存储了适当的任何类型的对象对齐,在数组分配开销此限制允许分配的字符数组到哪些对象等各类最终会放置的习惯用法。 ]

这两个给我的上述问题完全满意的答复,但...

语句1:
对X型的对象的对齐要求,其中的sizeof(X)== n至少是由N或类似的东西是X的地址整除的要求(把所有的体系结构相关的东西进入了之类的东西 )。

问题1:请确认,细化,或拒绝上述语句1

语句2:如果语句1是正确的,然后从第二个报价标准中可以得出500万字符数组被分配在地址整除5000000这是完全不必要的,如果我只需要字符数组这样,还不如一个原始存储的其他对象的可能位置。

问题2:那么,成功地分配1000个字符确实低于500短裤的机会(提供短则2个字节)?它是实际上是一个问题吗?

解决方案
  

为对象的对齐要求   X型的,其中的sizeof(X)== n为   中至少要求该地址   X为被n整除或者类似的东西   这

没有。一类型的对齐要求总是其大小的一个因素,但不必是等于其大小。它通常等于最大的类的所有成员的对齐要求。

500万字符数组,对自己的账户,只需要拥有1的对齐要求,一样的对齐要求单个字符

所以,文本你引用左右的内存,通过全球运营商分配的调整,(和的malloc 有类似的,尽管这个都不相同的要求)的效果意味着大量分配必须服从任何类型在系统的最严格的对齐要求。而且到,实现通常不包括大的单指令多数据类型从此,并要求内存SIMD进行特别分配。这是稍有可疑,但我认为他们证明它是非标准,扩展类型可以并处任意特殊要求的基础上。

因此​​,在实践中,你认为是500万的数量往往是4: - )

How is STL vector usually implemented? It has a raw storage of char[] which it occasionally resizes by a certain factor and then calls placement new when an element is pushed_back (a very interesting grammatical form I should note - linguists should study such verb forms as pushed_back :)
And then there are the alignment requirements. So a natural question arises how can I call a placement new on a char[] and be sure the alignment requirements are satisfied. So I searched the C++ standard of 2003 for the word "alignment" and found these:

Paragraph 3.9 Clause 5

Object types have alignment requirements (3.9.1, 3.9.2). The alignment of a complete object type is an implementation-defined integer value representing a number of bytes; an object is allocated at an address that meets the alignment requirements of its object type.

Paragraph 5.3.4 Clause 10:

A new-expression passes the amount of space requested to the allocation function as the first argument of type std::size_t. That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array. For arrays of char and unsigned char, the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the most stringent alignment requirement (3.9) of any object type whose size is no greater than the size of the array being created. [Note: Because allocation functions are assumed to return pointers to storage that is appropriately aligned for objects of any type, this constraint on array allocation overhead permits the common idiom of allocating character arrays into which objects of other types will later be placed. ]

These two give a perfectly satisfactory answer for my above question, but...

Statement1:
An alignment requirement for an object of type X where sizeof(X) == n is at least the requirement that address of X be divisible by n or something like that (put all the architecture-dependent things into the "or something like that").

Question1: Please confirm, refine, or deny the above statement1.

Statement2: If statement1 is correct then from the second quote in the standard it follows that an array of 5000000 chars is allocated at an address divisible by 5000000 which is completely unnecessary if I just need the array of char as such, not as a raw storage for possible placement of other objects.

Question2: So, are the chances of successfully allocating 1000 chars really lower than 500 shorts(provided short is 2 bytes)? Is it practically a problem?

解决方案

An alignment requirement for an object of type X where sizeof(X) == n is at least the requirement that address of X be divisible by n or something like that

No. The alignment requirement of a type is always a factor of its size, but need not be equal to its size. It is usually equal to the greatest of the alignment requirements of all the members of a class.

An array of 5M char, on its own account, need only have an alignment requirement of 1, the same as the alignment requirement of a single char.

So, the text you quote about the alignment of memory allocated via global operator new, (and malloc has a similar although IIRC not identical requirement) in effect means that a large allocation must obey the most stringent alignment requirement of any type in the system. Further to that, implementations often exclude large SIMD types from this, and require that memory for SIMD be specially allocated. This is slightly dubious, but I think they justify it on the basis that non-standard, extension types can impose arbitrary special requirements.

So in practice the number which you think is 5000000 is often 4 :-)

这篇关于字符数组的对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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