gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:47:23 25 4
gpt4 key购买 nike

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

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/

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