初始化一个指针但没有得到相同的值零

Initializing a pointer but not getting the same value zero

我的问题来自于在两个不同的编译器中编译下面的代码。我在 Eclipse Helios 和在线 GDB 编译器中输入了以下代码行,得到了不同的结果:

    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 现在在其中保存了一些垃圾值而不是零值吗?

Is it just telling me ptr1 is now holding some garbage value in it rather than zero?

是的,该值不能保证为零。对于 default initialization

(强调我的)

otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

所以两个编译器的观察结果都是合理的。