gpt4 book ai didi

c++ - std::allocator 中 "destroy" "destructor" "deallocate"之间的区别?

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

在 C++ std::allocator 中,有三个方法与一个共同的概念相关:

  • 解除分配
  • 销毁
  • 析构函数

我想知道:

  1. 从内存管理的角度来看,它们之间有何不同?
  2. 我什么时候应该使用这个而不是那个?

谢谢!


编辑:更具体的疑问:

一开始不好意思笼统地说,这里有一些我不明白的地方。

  1. 析构函数是做什么的?文档没有说到析构函数调用时内存是否会自动释放
  2. destroy用于调用对象的析构函数,这里的“对象”是什么意思?

再次感谢!

最佳答案

只是来自 cppreference.com documentation 的简短描述为我非常清楚地解释差异

enter image description here

"1. What does destructor do? The documentation didn't talk about whether the memory will be released automatically when the destructor is called"

std::allocator 实例占用的任何内存都将照常释放。

"2. The destroy is used to call the destructor on a object, what does the "object" here means?"

再次引用详细文档

 void destroy( pointer p ); // 1)
template< class U > // 2)
void destroy( U* p );

Calls the destructor of the object pointed to by p
1) Calls ((T*)p)->~T()
2) Calls p->~U()

Object 在此上下文中表示 T 类型的对象,由 std::allocator 实例管理。

关于c++ - std::allocator 中 "destroy" "destructor" "deallocate"之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26667026/

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