問題描述
我這里有一個非常簡單的 C++ 代碼:
I have a very simple C++ code here:
char *s = new char[100];
strcpy(s, "HELLO");
delete [] s;
int n = strlen(s);
如果我通過按 F5(開始調試)從 Visual C++ 2008 運行此代碼,這總是會導致崩潰(訪問沖突.)但是,在 IDE 之外啟動此可執行文件,或使用 IDE 的Ctrl+F5(不調試啟動)不會導致任何崩潰.可能有什么不同?
If I run this code from Visual C++ 2008 by pressing F5 (Start Debugging,) this always result in crash (Access Violation.) However, starting this executable outside the IDE, or using the IDE's Ctrl+F5 (Start without Debugging) doesn't result in any crash. What could be the difference?
我也想知道是否可以穩定重現訪問已刪除區域導致的訪問沖突崩潰?這種崩潰在現實生活中很少見嗎?
I also want to know if it's possible to stably reproduce the Access Violation crash caused from accessing deleted area? Is this kind of crash rare in real-life?
推薦答案
通過刪除的指針訪問內存是未定義行為.您不能期望任何可靠/可重復的行為.
Accessing memory through a deleted pointer is undefined behavior. You can't expect any reliable/repeatable behavior.
很可能在一種情況下它有效",因為字符串仍然坐在那里"在現在可用的內存中 -= 但你不能依賴它.VS 使用調試值填充內存以幫助強制崩潰以幫助查找這些錯誤.
Most likely it "works" in the one case because the string is still "sitting there" in the now available memory -= but you cannot rely on that. VS fills memory with debug values to help force crashes to help find these errors.
這篇關于取消引用已刪除的指針總是會導致訪問沖突?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!