I'm confused about some details of wild pointer and dangling pointer, here is my code:
#include <iostream>
using std::cout;
using std::endl;
class A
{
public:
void Func() { cout << "Func of class A, the address is:" << this <<endl; }
};
void Test(void)
{
A *p;
//get an error in VS2013 because of uninitialized pointer.
//But it's ok in g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
p->Func(); // wild pointer, ((1))
{
A a;
p = &a;
p->Func();
}
p->Func(); //dangling pointer ((2))
}
int main()
{
Test();
return 0;
}
The results are like follows:
Windows:
Func of class A, the address is:003CFD47
Func of class A, the address is:003CFD47
Ubuntu:
Func of class A, the address is:0xb74ef39d
Func of class A, the address is:0xbff85a3b
Func of class A, the address is:0xbff85a3b
My questions:
(1) the g++ compiler let the wile pointer pass at((1)), even when run the code, it seems point to 'some object'. why can this happen? Is it the bug of the compiler?
(2) As far as I know, after block sentences, p will be a dangling pointer at ((2)). But why can p go on point to Func()? Because the space occupied by object a is not overwrote by other application?