gpt4 book ai didi

c++ - 标准中哪里说 `new` 返回的每个分配都与 `std::max_align_t` 对齐?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:18 25 4
gpt4 key购买 nike

In this video ,在大约 6.39 处,演示者似乎在说 new 总是返回与 std::max_align_t 对齐的内存,这是有道理的,因为 operator new对分配的变量类型一无所知。也就是说,编译器必须选择最严格的对齐方式。但我在标准中找不到这个。

演示者还说,当 new 用于分配 charunsigned char 数组时,此规则不适用。在这种情况下,对齐取决于大小。但这对我来说也不清楚。

最佳答案

这是在 [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). [...]

关于c++ - 标准中哪里说 `new` 返回的每个分配都与 `std::max_align_t` 对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com