May be this is very dumb question. But I don't know answer to this.
If object to which pointer is referring is deleted by someone( we don't know who is deleting it ), but I still have raw pointer to it. The how to validate if its a valid or not. I though null check would help, but it does not.
Here is an example:
int main()
{
class A
{
public:
A() = default;
~A() = default;
private:
int a;
long b;
};
A* a = new A();
delete a; // deleted here
A* b = reinterpret_cast< A* >( a ); // still trying to use it
if( b == nullptr )
std::cout << "B is Null"; // b should have come as null, but it does not and I do not see this line in output.
// same here
A* c = ( A* ) a;
if( c == nullptr )
std::cout << "C is null";
getchar();
return 0;
}
If you see in above picture, a is not null, and has some garbage values.
I know this is very naïve code, but this show problem that I have with actual code.
Thanks, Kailas