【发布时间】:2019-08-25 04:09:20
【问题描述】:
我的问题是由于在两个单独的编译器中编译下面的代码而产生的。我在 Eclipse Helios 和 Online GDB Compiler 中输入了以下代码行,得到了不同的结果:
int* ptr1 = new int;
int* ptr2 = new int(20);
cout << "Value of ptr1 = " << *ptr1 << "\n";
cout << "Value of ptr2 = " << *ptr2 << "\n";
delete ptr1; // Destroying ptr1
delete ptr2; // Detroying ptr2
对于在线 GDB,结果是:
Value of ptr1 = 0
Value of ptr2 = 20
然而,Eclipse Helios的结果是:
Value of ptr1 = 225472
Value of ptr2 = 20
它只是告诉我 ptr1 现在在其中包含一些垃圾值而不是零吗?
【问题讨论】:
-
读取一个未初始化的变量是Undefined Behaviour。
标签: c++ pointers initialization delete-operator