c++ - 是否可以以不会导致 UB 的方式分配未初始化的数组?

标签 c++ memory-management dynamic-memory-allocation void-pointers reinterpret-cast

在 C++ 中实现某些数据结构时,需要能够创建一个包含未初始化元素的数组。正因为如此,拥有

buffer = new T[capacity];
不适合,如 new T[capacity]初始化数组元素,这并不总是可能的(如果 T 没有默认构造函数)或需要(因为构造对象可能需要时间)。典型的解决方案是分配内存并使用placement new。
为此,如果我们知道元素的数量是已知的(或者至少我们有一个上限)并在堆栈上分配,那么,据我所知,可以使用对齐的字节或字符数组,然后使用std::launder访问成员。
alignas(T) std::byte buffer[capacity];
但是,它只解决了栈分配的问题,并没有解决堆分配的问题。为此,我假设需要使用对齐新,并写这样的东西:
auto memory =  ::operator new(sizeof(T) * capacity, std::align_val_t{alignof(T)});
然后将其转换为 std::byte*unsigned char*T* .
// not sure what the right type for reinterpret cast should be
buffer = reinterpret_cast(memory);
但是,有几件事我不清楚。
  • 结果reinterpret_cast<T*>(ptr)如果 ptr 指向可与 T 指针互转换的对象,则定义该对象。(参见 this answerhttps://eel.is/c++draft/basic.types#basic.compound-3 )了解更多详细信息。我假设,将其转换为 T*无效,因为 T 不一定可以与 new 的结果进行指针转换。但是,对于 char* 是否有很好的定义?或 std::byte ?
  • 转换 new 的结果时到一个有效的指针类型(假设它不是实现定义的),它是否被视为指向数组第一个元素的指针,或者只是指向单个对象的指针?虽然据我所知,在实践中很少(如果有的话)很重要,但存在语义差异,即 pointer_type + integer 类型的表达式。只有当所指向的元素是数组成员,并且算术结果指向另一个数组元素时,才定义良好。 (见 https://eel.is/c++draft/expr.add#4)。
  • 就生命周期而言,数组类型的对象unsigned charstd::byte可以为放置 new ( https://eel.is/c++draft/basic.memobj#intro.object-3 ) 的结果提供存储,但是它是否为其他类型的数组定义?
  • 据我所知T::operator newT::operator new[]表达式调用 ::operator new::operator new[]在幕后。由于内置的​​结果 new是无效的,如何转换为正确的类型?这些是基于实现的还是我们有明确定义的规则来处理这些?
  • 释放内存时,应该使用
  • ::operator delete(static_cast<void*>(buffer), sizeof(T) * capacity, std::align_val_t{alignof(T)});
    
    还是有另一种方法?
    PS:我可能会在实际代码中将标准库用于这些目的,但是我试图了解幕后的工作原理。
    谢谢。

    最佳答案

    pointer-interconvertibility


    关于指针相互转换,是否使用 T * 无关紧要或 {[unsigned] char|std::byte} * .您必须将其转换为 T *无论如何都要使用它。
    请注意您必须调用 std::launder (根据转换的结果)访问尖头 T对象。唯一的异常(exception)是创建对象的placement-new 调用,因为它们还不存在。手动析构函数调用也不异常(exception)。
    如果您不使用 std::launder,则缺少指针可相互转换只会是一个问题。 .

    When converting the result of new to a valid pointer type (assuming it is not implementation defined), is it treated as a pointer to first element of array, or just a pointer to a single object?


    如果您想更加安全,请将指针存储为 {[unsigned] char|std::byte} *reinterpret_cast它在执行任何指针运算之后。

    an object of type array unsigned char or std::byte can provide storage for result of placement new


    该标准没有说任何地方都需要“提供存储”才能使新放置工作正常工作。我认为该术语的定义仅用于标准中其他术语的定义。
    考虑 [basic.life]/example-2 哪里operator=即使输入 T,也使用placement-new 就地重建对象。不为相同类型“提供存储”T .

    Since the result of builtin new is void, how conversion to the right type is done?


    不知道标准对此有什么看法,但除了 reinterpret_cast 之外还能有什么? ?

    freeing the memory


    你的方法看起来是正确的,但我 think你不必传递大小。

    关于c++ - 是否可以以不会导致 UB 的方式分配未初始化的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66624147/

    相关文章:

    C 指针和内存分配 : Realloc Arrays and Pointer Passing

    c++ - 复制构造函数应该复制 *string 值

    c - 内存分配问题

    c++ - 拆分函数的声明和定义,还是删除函数头中的新运算符?

    c++ - 如何将 D3DVERTEXELEMENT9 声明移动到 header

    windows - 从私有(private)堆分配内存会导致死锁吗?

    c - Valgrind 内存泄漏报告中的时间戳不正确

    使用qt的c++应用程序,如何包含gnuplot

    c++ - LLVM:将全局变量链接类型设为 linkonce

    c - 将指针添加到指针数组