标准全局默认运算符new的对齐限制是什么? [英] What are the alignment limitations of the standard global default operator new?

查看:495
本文介绍了标准全局默认运算符new的对齐限制是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些使用ATL的CComBSTR类型的旧代码.我对其进行了更改,以使其可以使用Visual C ++ Express Edition进行编译,而Visual C ++ Express Edition不随ATL一起提供.我只使用了CComBSTR的一个很小的子集,所以这样做非常简单.

I'm working on some older code that uses ATL's CComBSTR type. I'm changing it so that it will compile using Visual C++ Express Edition, which does not come with ATL. I used only a very small subset of CComBSTR, so doing this is fairly simple.

但是,在分配BSTR内存块时,我需要使用4字节长的前缀填充前四个字节.我担心如果使用new char[size]表达式为字符串分配内存,由于分配的char数组对四个字节的前缀没有正确的对齐方式,将导致对齐错误.

However, when allocating the BSTR memory block, I need to fill the first four bytes with a 4 byte length prefix. I'm concerned that if I use a new char[size] expression to allocate the memory for the string, that I will cause alignment faults due to the allocated char array not having the correct alignment for the four byte prefix.

标准中是否有任何内容说明new的返回值具有什么对齐要求?我在C ++ 11中看到的全部是:

Is there anything in the standard that states what alignment requirements the returned values of new have? All I see in C++11 are:

5.3.4/1 [expr.new]
是否支持过度对齐类型是由实现定义的(3.11).

5.3.4/1 [expr.new]
It is implementation-defined whether over-aligned types are supported (3.11).

3.11/6 [basic.align]
可以使用alignof表达式(5.3.6)查询完整类型的对齐要求.此外,char,signed char和unsigned char类型的对齐要求最弱. [注意:这使字符类型可以用作对齐的内存区域(7.6.2)的基础类型.

3.11/6 [basic.align]
The alignment requirement of a complete type can be queried using an alignof expression (5.3.6). Furthermore, the types char, signed char, and unsigned char shall have the weakest alignment requirement. [ Note: This enables the character types to be used as the underlying type for an aligned memory area (7.6.2).—end note ]

我觉得这有点令人困惑-最弱的对齐要求"对我说对对齐的最严格限制",但是下面的注释似乎表明标准的含义相反.

I find this slightly confusing -- "weakest alignment requirement" says to me "least strict constraint on alignment", but the note under this seems to indicate the standard means the opposite.

我可以像这样将new char[sizeof(uint32_t) + 2*(length + 1)]缓冲区用作BSTR吗?

Am I safe using a new char[sizeof(uint32_t) + 2*(length + 1)] buffer as a BSTR like this?

EDIT :我刚刚意识到,在BSTR的这种特定情况下,无论如何都要分配字符串来使用SysAllocString.但我仍然对以这种方式使用new是否可以使用

EDIT: I just realized that in this specific case of BSTR, one needs to use SysAllocString in order to allocate the string anyway; but I'm still interested in whether or not it is okay to use new in this way.

推荐答案

5.3.4/1 [expr.new]

5.3.4/1 [expr.new]

由实现方式定义是否支持过度对齐的类型(3.11).

It is implementation-defined whether over-aligned types are supported (3.11).

这里有一件重要的事情:over-aligned意味着比任何内置类型都更加对齐.例如,在64位计算机上,指针通常对齐8个字节,因此在那些计算机上,对齐过度意味着意味着对齐严格大于8.

One important thing here: over-aligned means more aligned than any built-in type. For example, on 64 bits machine, pointers are generally 8 bytes aligned and thus on those machines over-aligned means having an alignment strictly greater than 8.

因此,over-aligned仅在使用向量类型(例如SSE或AVX指令所需的向量类型或C/C ++的某些变体(例如Open CL))时需要考虑.在日常编程中,您使用内置类型编写的类型永远不会过度对齐.

Therefore, over-aligned is only of concern when using vector types, such as those required for SSE or AVX instructions or some variants of C/C++ (like Open CL). In day to day programming, the types you craft from the built-in types are never over-aligned.

§3.11对齐方式[basic.align]

3/ 扩展的比对由大于alignof(std::max_align_t)的比对表示.由实现定义,是否支持任何扩展的对齐方式以及所支持的上下文(7.6.2).具有扩展对齐要求的类型是过度对齐类型.

3/ An extended alignment is represented by an alignment greater than alignof(std::max_align_t). It is implementation-defined whether any extended alignments are supported and the contexts in which they are supported (7.6.2). A type having an extended alignment requirement is an over-aligned type.

9/如果实现不支持在特定上下文中请求特定扩展对齐的请求,则程序格式错误. 此外,无法兑现请求的对齐方式的动态存储的运行时分配请求应视为分配失败.

9/ If a request for a specific extended alignment in a specific context is not supported by an implementation, the program is ill-formed. Additionally, a request for runtime allocation of dynamic storage for which the requested alignment cannot be honored shall be treated as an allocation failure.

此外,new习惯上返回与alignof(std::max_align_t)对齐的内存.这是因为常规::operator new仅知道要分配的对象的大小,而不是其对齐方式,因此需要满足程序中可能的最强对齐要求.

Furthermore, it is customary for new to return memory aligned to alignof(std::max_align_t). This is because the regular ::operator new is only aware of the size of the object to allocate for, not of its alignment, and therefore need satisfy the strongest alignment requirements possible in the program.

另一方面,当心在堆栈上分配的char数组,不能保证其对齐方式最终会是什么.

On the other hand, beware of a char array allocated on the stack, there is no guarantee what its alignment would end up being.

这篇关于标准全局默认运算符new的对齐限制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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