In this video , at approximately 6.39, the presenter seems to say that new
always returns memory aligned to std::max_align_t
, which makes sense, since operator new
doesn't know anything about the type of the variable being allocated. That is, the compiler has to opt for the strictest alignment. But I can't find this in the Standard.
The presenter also says that this rule doesn't apply when new
is used to allocate char
or unsigned char
arrays. In this case, the alignment depends on the size. But this was not clear to me either.
This is in [basic.stc.dynamic.allocation]/2:
The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. [...] The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function). [...]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.