0

C++ 17 introduces __STDCPP_DEFAULT_NEW_ALIGNMENT__ which defines the guaranteed alignment when uses not new-extended operator new. However, std::max_align_t is 8 which means the maximum alignment of basic types is 8 and __STDCPP_DEFAULT_NEW_ALIGNMENT__ over-aligned. My question is why MSVC and other compilers want to over-aligned? Any instruction is being used underlying requires this over-aligned alignment?

Similar question was asked in the following link before:

Why does the C++ standard allow std::max_align_t and __STDCPP_DEFAULT_NEW_ALIGNMENT__ to be inconsistent?

However, the comments only mentions that

"Because types that require 16-byte alignment existed over a decade before C++'s default new provided a mechanism to explicitly request allocations of such alignments. In order to allow users to use such types, Microsoft decided to make their default allocator always return 16-byte-aligned pointers."

I am wondering what's the "such types" specifies in detail?

user438383
  • 5,716
  • 8
  • 28
  • 43
Tinggo
  • 1,127
  • 1
  • 9
  • 18
  • "*what's the "such types" specifies in detail?*" What do you mean by that? It's not clear what you're looking for. – Nicol Bolas May 18 '22 at 15:15
  • 1
    even function call stacks have been aligned to at least 16 bytes for decades [Why does the Mac ABI require 16-byte stack alignment for x86-32?](https://stackoverflow.com/q/612443/995714), [Why does the AMD64 System V ABI mandate a 16 byte stack alignment?](https://stackoverflow.com/q/49391001/995714), [Is the Microsoft Stack always aligned to 16-bytes?](https://stackoverflow.com/q/52615235/995714). [Why should data be aligned to 16 bytes for SSE instructions](https://community.intel.com/t5/Software-Tuning-Performance/Why-should-data-be-aligned-to-16-bytes-for-SSE-instructions/m-p/1164004) – phuclv May 18 '22 at 15:52
  • [Why is dynamically allocated memory always 16 bytes aligned?](https://stackoverflow.com/q/59098246/995714), [Why is malloc 16 bytes alligned](https://stackoverflow.com/q/70692795/995714), [Why is stack frame a multiple of 16 bytes long?](https://stackoverflow.com/q/35700491/995714) – phuclv May 18 '22 at 16:04

1 Answers1

1

MSVC defines __STDCPP_DEFAULT_NEW_ALIGNMENT__ to 16 because its operator new provides this alignment guarantee.

On MSVC, operator new eventually calls HeapAlloc, and according to the documentation, the alignment of memory returned by HeapAlloc is 16 on Win64.

The alignment of memory returned by HeapAlloc is MEMORY_ALLOCATION_ALIGNMENT in WinNT.h:

#if defined(_WIN64) || defined(_M_ALPHA)
#define MEMORY_ALLOCATION_ALIGNMENT 16
#else
#define MEMORY_ALLOCATION_ALIGNMENT 8
#endif
cpplearner
  • 13,776
  • 2
  • 47
  • 72