不调用对象的析构器是未定义的行为吗?

问题描述 投票:0回答:1

这似乎是一个显而易见的问题,但是我一直没有找到明确的答案。请看下面的代码。

{
    std::aligned_storage_t<sizeof(int), alignof(int)> storage;
    int& i = *new(&storage) int;
}

在这里,我启动了一个名为 "对象 "的生命。i 但这一生并没有明确的 "结束",因为不应该有任何的呼唤 ~int.我不知道这个问题的答案是否取决于类型是否琐碎,所以我将提供这第二个例子。


class MyMem : public std::pmr::memory_resource
{
    std::size_t mem_size = 0u;
    char* mem_handle = nullptr;

    virtual void* do_allocate(std::size_t bytes, std::size_t) final
    {
        mem_size += bytes;
        mem_handle = static_cast<char*>(std::realloc(mem_handle, mem_size));
        return mem_handle + mem_size - bytes;
    }
    virtual void do_deallocate(void*, std::size_t, std::size_t) final {}
    virtual bool do_is_equal(const std::pmr::memory_resource&) const noexcept { return false; }

public:
    void* data() const noexcept { return mem_handle; }
};

//...
MyMem mbr;
{
    std::aligned_storage_t<sizeof(std::pmr::vector<int>), alignof(std::pmr::vector<int>)> vec_storage;
    auto& vec = *new(&vec_storage) std::pmr::vector<int>(&mbr);
    vec.resize(N);
}
// Free the data here
std::free(mbr.data());

使用一个自定义的内存资源,我可以得到一个句柄 到数据分配的调用到 vector::resize 并确保我们不会泄露内存。然而实际的destructor调用的是 vector 缺少,因此每个 int 对象分配在该内存中。

c++ c++17 language-lawyer destructor
1个回答
1
投票

不,这不是未定义的行为。一个对象的析构器不需要被调用。(C++标准例子)

N.B: 然而,在你的例子中,由 placement new 表达式创建的 int 对象的生命期在结束括号处结束。如果一个对象的析构器被调用,或者它的存储被释放或重新使用,那么这个对象的生命期就结束了。[基本生活]1:

{
  std::aligned_storage_t<sizeof(int), alignof(int)> storage;
  int& i = *new(&storage) int;
} // storage released => end of life of the int.

{
  std::aligned_storage_t<sizeof(std::pmr::vector<int>),
                                alignof(std::pmr::vector<int>)> vec_storage;
  auto& vec = *new(&vec_storage) std::pmr::vector<int>(&mbr);
  vec.resize(N);
} // end of life of the vector, without destructor call

{
  std::aligned_storage_t<sizeof(std::pmr::vector<int>),
                                alignof(std::pmr::vector<int>)> vec_storage;
  auto& vec = *new(&vec_storage) std::pmr::vector<int>(&mbr);
  vec.resize(N);
  // next statement end the life of the vector refered by vec, no destructor called
  auto& vec2 = *new(&vec_storage) std::pmr::vector<int>(&mbr);
} // end of life of the vector referred by vec2, no destructor called

N.B.2:在最后一个块中,由于没有调用destructor,你会有内存泄漏。但允许内存泄漏。

© www.soinside.com 2019 - 2024. All rights reserved.