5

What is the boolean value that is returned when a fucntion call is made on a null object in C++?

ClassDummy* dummy = NULL;
if (!dummy->dummy_function(1,2,3)) {
  // Do Something
}

Shouldn't this return an error according to C++11 standards?

ibp73
  • 650
  • 1
  • 6
  • 16
  • And to answer all your future "shouldn't C++ tell me I'm doing something wrong" questions in advance: no. C++ optimizes heavily for the case where your program works, at the expense of saying almost nothing about the case where you have a bug. – user2357112 Jun 08 '14 at 08:44

5 Answers5

5

Unless dummy has been declared at namespace scope, it is uninitialized and its value is unspecified, i.e. it may or may not be null. Invoking a member function on a nullptr, or on a pointer that is pointing to invalid memory, is undefined behavior.

You might get away with the correct result if the member function you invoke doesn't access any other data members of the class; in other words, if it doesn't dereference the this pointer. However, regardless of whether you obtain the expected result or not, it is still undefined behavior.

Compilers are not required to detect such invalid function calls. If you do something obvious, like initialize an object pointer to nullptr and then invoke a member function on it, you may see a diagnostic from the compiler, but this is not guaranteed.

Community
  • 1
  • 1
Praetorian
  • 106,671
  • 19
  • 240
  • 328
4

In the original question dummy had an unspecified value, maybe null. The effect after the edit isn't changed, though: The behavior is undefined. Its not an error any tool is required to detect: it is the mandate of the programmer to call member functions only on valid objects.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • I have changed the question a little bit. Would there be an error if the pointer is explicitly marked NULL? – ibp73 Jun 08 '14 at 01:50
  • 4
    nope. still the same. ... and if you need clarification on the meaning of [undefined](http://dspace.dial.pipex.com/town/green/gfd34/art/bloopers.html): follow the link! – Dietmar Kühl Jun 08 '14 at 01:51
  • @DietmarKühl, Wow, I rather like that one. – chris Jun 08 '14 at 03:07
2

Calling a method through a null pointer has undefined behavior. The code is invalid, but the compiler is not required to tell you that it is.

The C++ standard defines many cases where, even though the code is invalid, the compiler is not required to give a "diagnostic". In many cases, the reason is just because it is very difficult for the compiler to determine whether or not the code is valid. In your particular code, this is pretty easy, and some compilers may in fact give a warning about it if you use the proper warning level. However, it wouldn't be too difficult to construct a more complicated example where the compiler wouldn't easily be able to tell whether the pointer was null or not. Standardizing exactly how complex the code has to be before the compiler doesn't need to give a diagnostic would be difficult, and probably pointless, so it is just left up to each implementation to make that decision.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
2

Trying to dereference a null pointer like this will result in undefined behavior.

(Typically, a core dump or memory access exception on most systems.)

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

Calling non-static member functions and accessing non-static member variables should produce an error.

Some compilers and run time environments allow use of static member variables and member functions with a NULL pointer but that is still undefined behavior according to the standard.

See C++ static const access through a NULL pointer and the accepted answer for more details.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270