c++ - 堆上数组解除分配的具体查询

标签 c++ memory-management compiler-construction heap-memory

当在某个点传递堆分配的数组时,我们调用:

delete [] ptrName

编译器如何记住需要释放多少内存?

上下文:

我们有几个程序会抛出指向堆上字节数组的指针,出于各种原因在 int32、int8 和其他适当的类型之间转换。当内存需要通过 delete 调用取消分配时,到底要做什么来确保释放正确的内存量?

谢谢

最佳答案

编译器分配x字节的开销,其中有分配的内存信息,它知道调用delete []时要删除多少。

§ 5.3.4 新

  • new T[5] 导致调用 operator new,并且
  • new(2,f) T[5] 导致调用 operator new。

Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operator new[]. This overhead may be applied in all array new-expressions, including those referencing the library function operator new[](std::size_t, void*) and other placement allocation

关于c++ - 堆上数组解除分配的具体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624215/

相关文章:

c++ - 获取年龄过时的结构

ios - 属性、线程、内存管理和摇滚

c++ - 新数组放置需要缓冲区中未指定的开销?

c# - 如何存储对静态类的引用?

compiler-construction - 为什么 Julia 编译器不优化这个循环?

c++ - 函数指针中的参数列表

c++ - 询问输入数据的数量并将输入数据存储在数组中

c++ - 如何获取非类型模板参数的值?

objective-c - 什么时候需要 NS_RETURNS_RETAINED?

新操作系统的C语言编译器(理论问题)?