What are the alignment limitations of the standard global default operator new?

こ雲淡風輕ζ 提交于 2019-12-05 05:54:07

5.3.4/1 [expr.new]

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

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.

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 Alignment [basic.align]

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/ 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.

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.

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

It is an implementation detail, but MSVC uses the operating system allocators. HeapAlloc() for CRT allocations, CoTaskMemAlloc() for COM type wrappers like _bstr_t. They both align by 8, both in 32-bit and 64-bit code.

You should never allocate memory for BSTRs with the new operator, the COM allocator must be used to ensure that they get deallocated using the proper heap. Important in any interop scenario, which is where BSTR is used, it is a standard Automation type. CoTaskMemAlloc/Free() is required but always use the BSTR helper functions to ensure they get properly initialized. SysAllocString() and SysFreeString(). Use SysAllocStringLen() to deal with strings containing embedded zeros.

You should never try to use C++ memory management functions for BSTRs - they should only be allocated using SysAllocString() family functions. This ensures that whoever obtains a BSTR can use SysFreeString() and other functions of the family on the obtained BSTR. If you violate this requirement your program will run into undefined behavior.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!