Which object's draw is being called in case of pointer pointing to NULL?

http://liveworkspace.org/code/lDYbz$0
Which object's draw is being called?
There is no object. You are dereferencing a null pointer which is "bad" and results in undefined behavior -- possibly a program crash.
but program isn't crashing.
Behavior is undefined. That means anything could happen.

Maybe it will crash. Maybe it won't.
There are no virtual functions, so there is no check of the vtable. Because of this, it just calls a function based on the static type. Since the function doesn't use the this pointer, no undefined behavior occurs nothing bad happens.

In other words, the implementation doesn't check the pointer, so it is never even noticed that it is null. The behavior is still undefined.
Last edited on
Since the function doesn't use the this pointer, no undefined behavior occurs.


There was discussion on this before. I use to have this stance but was proven wrong.

If you're using ->, you are dereferencing a pointer. Dereferencing a null pointer is undefined behavior. As far as the standard is concerned, it's that cut-and-dry.

Though from a practical standpoint, you are right. The compiler does not need to actually dereference 'this' which is why it's not crashing for him. But there is no guarantee that will always be the case.
I never said it wasn't undefined - I guess what I meant was 'nothing bad happened' rather than 'no undefined behavior occurred'. You're right, the behavior is undefined and I used the wrong words.
Topic archived. No new replies allowed.