[英]C ++ exit code 3221225477
I am trying to run a simple C++ program on pointers but the output I am getting is " [Finished in 5.55s with exit code 3221225477] ".我正在尝试在指针上运行一个简单的 C++ 程序,但我得到的 output 是“ [在 5.55 秒内完成,退出代码为 3221225477] ”。 The Norton Data Protector on my system is blocking the file execution (I guess) but I don't know why?
我系统上的 Norton Data Protector 阻止了文件执行(我猜),但我不知道为什么? I deactivated the Data Protector but nothing changed.
我停用了 Data Protector,但没有任何改变。 Kindly someone please take a look at the my code and explain to me, what is going on here?
请有人看一下我的代码并向我解释一下,这里发生了什么? Thank You
谢谢你
#include <iostream>
using namespace std;
int main()
{
int x;
int * ptr ;
*ptr = x ;
cout<<ptr;
}
The problem is that the variable x
is unitialized and you're using the value of an uninitialized variable which leads to undefined behavior.问题是变量
x
是未初始化的,并且您正在使用未初始化变量的值,这会导致未定义的行为。 Same goes for variable ptr
, it is also uninitialized and dereferencing it leads to undefined behavior.变量
ptr
也是如此,它也未初始化并且取消引用它会导致未定义的行为。
*ptr = x ;//undefined behavior because first x is uninitialized and you're using x and second ptr is also unitialized and you're dereferencing ptr
You should do:你应该做:
int x = 0;
int *ptr = &x;//now ptr is a pointer pointing to variable x
For this very reason it is advised that:出于这个原因,建议:
always initialize built in types in local/block scope otherwise they have garbage value and using/accessing that garbage value can lead to undefined behavior
始终初始化本地/块 scope 中的内置类型,否则它们具有垃圾值并且使用/访问该垃圾值可能导致未定义的行为
UB(short for undefined behavior) means anything can happen Including but not limited to access violation error . UB(未定义行为的缩写)表示任何事情都可能发生,包括但不限于访问冲突错误。 Check out What does access violation mean?
查看访问冲突是什么意思? .
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.