Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以清除损坏的堆?_C++_Memory Management_Memory Leaks_Free_Delete Operator - Fatal编程技术网

C++ 是否可以清除损坏的堆?

C++ 是否可以清除损坏的堆?,c++,memory-management,memory-leaks,free,delete-operator,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等将导致上述代码崩溃。记忆是否仍能以某种方式被释放,或者它是否已经被打破,超出了希望 如果我弄坏了我的垃圾堆,我可以事后清理吗 不,你不

如果我弄坏了我的垃圾堆,我可以事后清理吗?如果是,如何进行

堆损坏的示例:

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
等将导致上述代码崩溃。记忆是否仍能以某种方式被释放,或者它是否已经被打破,超出了希望

如果我弄坏了我的垃圾堆,我可以事后清理吗

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

无法撤消
*x=1或以还原以前驻留在那里的值


然而,调用free(x)、delete x等将导致上述代码崩溃

当然,你在试图释放一些你从未分配的东西

记忆是否仍能以某种方式被释放,或者它是否已经被打破,超出了希望


它被打破了,没有任何希望。

它被摧毁了。跑了。重新开始,不要再做这些事情。通常不会,一旦你损坏了内存,就真的无法回到以前的状态。谢谢你的快速回复。我稍后会接受你的回答。但我认为有一些东西是分配的——x的大小实际上是8。它应该只有4号。它现在崩溃了,是因为它预期大小为4,还是因为第二个整数的地址以前没有标记为已分配内存?@Imago
new
通常在后台分配较大的内存块,但是您的代码有未定义的行为,并且您不允许依赖这些实现细节。我看到了:/,我只是想更好地理解指针的概念。。我读到了指针,想知道会发生什么。。大多数教程似乎没有详细介绍。。