如果我损坏了堆,我可以随后清理它吗?如果是,怎么办?
堆损坏示例:
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/