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

标签 c windows winapi memory

假设以下场景:

  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/

相关文章:

c - 文本文件中字符串中的两个单词

c - Memcpy func 获取指针变量?字符*p;字符* q; memcpy(p,q,10);会起作用吗?

windows - 如何显示 "create shortcut"对话框?

连接到套接字被拒绝

c++ - Windows进程管理

reactjs - 是否有可能使React-Electron Standalone应用程序始终保持领先地位?

python Windows "ImportError: DLL load failed: The specified module could not be found."加载已编译扩展时

windows - 复选框 - 更改通知

c++ - ShellExecute() 不适用于 .bat 文件

c - UART pic18 到 pic18