gpt4 book ai didi

c - HeapReAlloc 改变内存地址后是否应该使用HeapFree?

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:52 25 4
gpt4 key购买 nike

假设以下场景:

  1. 我们使用 HeapAlloc() WINAPI 函数分配一 block 内存给 var X

  2. 我们想使用 HeapRealloc()

  3. 重新分配 X
  4. HeapRealloc() 调整堆的大小并移动到新位置,因此指针已更改 - 我们称它为 Y

我们应该在旧内存地址 (X) 被 HeapReAlloc 移动后对它执行 HeapFree 还是 HeapReAlloc 自动清理以前的我们的内存指针?

最佳答案

没有。 HeapReAlloc() 将已经释放旧地址。事实上,您会遇到的每个分配器都会这样做,无论是 Windows API 堆函数还是标准的 C malloc()/realloc()/free()。这是来自 the HeapReAlloc() documentation 的直接引述, 不过, 好的措施:

If HeapReAlloc fails, the original memory is not freed, and the original handle and pointer are still valid.

HeapReAlloc is guaranteed to preserve the content of the memory being reallocated, even if the new memory is allocated at a different location. The process of preserving the memory content involves a memory copy operation that is potentially very time-consuming.

这些结合起来应该得出这样的结论:如果成功,HeapReAlloc() 会释放旧的内存地址。

关于c - HeapReAlloc 改变内存地址后是否应该使用HeapFree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281722/

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