我这里有一个非常简单的 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模板网!
std::reference_wrapper 和简单指针的区别?Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和简单指针的区别?)
常量之间的区别.指针和引用?Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
c++ - 如何从指向向量的指针访问向量的内容?How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何从指向向量的指针访问向量的内容?)
*& 的含义和**&在 C++ 中Meaning of *amp; and **amp; in C++(*amp; 的含义和**amp;在 C++ 中)
为什么我不能对普通变量进行多态?Why can#39;t I do polymorphism with normal variables?(为什么我不能对普通变量进行多态?)
C++ 中的指针比较是未定义或未指定的行为吗?Is pointer comparison undefined or unspecified behavior in C++?(C++ 中的指针比较是未定义或未指定的行为吗?)