c++ - 损坏的堆可以清理吗?

标签 c++ memory-management memory-leaks free delete-operator

如果我损坏了堆,我可以随后清理它吗?如果是,怎么办?

堆损坏示例:

int *x = new int; // If we allocate memory wit new, we have to free it later.. 
x++;
*x = 1; // the heap is now corrupted

但是,调用free(x)delete x等将会导致上面的代码崩溃。内存是否仍然可以以某种方式释放,或者它是否已经被破坏得不可救药?

最佳答案

If I corrupt my heap, can I clean it up afterwards?

不,你无法解决这个问题。基本上你的程序有未定义的行为堆损坏是由一些在 Debug模式下运行代码的清理工具检测到的。

无法撤消 *x = 1; 或恢复之前所在的值。


However, calling free(x), delete x etc. will crash above's code.

当然,您正在尝试释放从未分配过的东西。

Can the memory still be somehow deallocated or is it already broken beyond hope?

一切都破灭了,没有任何希望。

关于c++ - 损坏的堆可以清理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54973082/

相关文章:

c++ - 包括多个动态数组的 c-struct 的分配

c++ - 允许重新申报?

c++ - AQTime DLL 分析 - 无结果

C++ Valgrind 会将剩余的分配内存检测为泄漏吗?

C++ 在 OS X 中移植 VirtualFree

objective-c - iOS SDK : first advice for beginners

java - JVM 中的内存分配

iphone - 我还能如何避免泄漏这个 Core Foundation 对象?

c++ - 堆密集型 C++ 程序

c++ - Linux MAKEFILE 访问环境变量