Can "this" be NULL?

Started by
24 comments, last by Zahlman 14 years ago
Quote:Original post by nerijus ramanauskas
Only if there is a polymorphism, compiler is unable to do this, because it simply cannot know where is your memory.


It will PROBABLY be unable to do this, but that isn't a certainty.

The reason being, most compilers (that i know of anyways!) implement the vtable by having a hidden pointer at the beginning of your objects memory which points to the classes vtable.

However, the implemenation of how that works isn't specified in the standards so each compiler can implement it differently if they wish to.

So, one compiler might implement it in a way where in the example case, using polymorphism will work just fine :P

Minor trivia but knowledge is good so figured id clear that up (even though others have hinted at it)
Advertisement
Quote:Original post by nerijus ramanauskas
Tell me why p->do(); should crash.


It doesn't need to crash, it is undefined behavior.

There is no reason why compiler couldn't raise an exception, or terminate an application in this case.

One reason why it cannot work is this:
Foo * foo = NULL;foo->bar();   // undefined// is same as(*foo).bar(); // illegal, foo is not dereferencable


Of course, NULL pointer, or value 0 might be a perfectly valid address.
Quote:Original post by Antheus
There is no reason why compiler couldn't raise an exception, or terminate an application in this case.

Absolutely agree, but there is also no reason why compiler couldn't simply optimize it "out".
Quote:Original post by nerijus ramanauskas
Quote:Original post by Antheus
There is no reason why compiler couldn't raise an exception, or terminate an application in this case.

Absolutely agree, but there is also no reason why compiler couldn't simply optimize it "out".


The compiler is not required to optimize things out just because it can. In practice, in many cases it does (otherwise people would use the competitor's compiler instead), but code that expects the compiler to perform an optimization is still incorrect.

If your code doesn't actually require the this-pointer, why is it in a member function?

[Edited by - Zahlman on March 31, 2010 2:14:15 PM]
Quote:
Original post by Zahlman
code that expects the compiler to perform an optimization is still incorrect.

Hey, no one said it was recommended to use null pointers in "real life". That's of course unrecommended to expect the compiler to perform an optimization, especially when VC++ turns it off when debugging.
Quote:Original post by Antheus
Foo * foo = NULL;foo->bar();   // undefined// is same as(*foo).bar(); // illegal, foo is not dereferencable


My stomach tells me there is absolutely no semantic difference whatsoever between foo->bar() and (*foo).bar(). Is it wrong?
If the member function you call from a NULL pointer, does NOT use / modify any data members, why would it not be possible? It would be like calling a regular function. Of course, the member function could be made static, but _technically_ it should be possible to do the following:

class A{public:    int a()    {        return 5;    }};A *a = NULL;std::cout << a->a();
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
but _technically_ it should be possible to do the following:

Only when the moon is full and your nasal demons are sleeping. The implementation details you are envisioning are not guaranteed by the standard.
I am going to go away from the common consensus here and state that a this pointer can never be NULL.
A this pointer is available in the body of a none static member, constructor or destructor. To be defined behavior by the standard, the pointer used to call the class is required to be a valid pointer of the correct type or derived type. Therefore according to the standard the this pointer can not be null.
Quote:Original post by DevFred
Quote:Original post by Antheus
...// is same as...


My stomach tells me there is absolutely no semantic difference whatsoever between foo->bar() and (*foo).bar(). Is it wrong?

You are right -- at least when dealing with true pointers.

This topic is closed to new replies.

Advertisement