Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C++ equivalent of a NullPointerException

Both Java and C#, and probably many other languages too, have a predefined exception class that is thrown when a null parameter is used where it should not. Is there anything similar in C++? If not, is there another predefined exception I can use or should I define my own one?

like image 935
Janina Petersen Avatar asked Jan 03 '13 12:01

Janina Petersen


People also ask

Does C have a NullPointerException?

There's no such thing as "null pointer exception" in C++. The only exceptions you can catch, is the exceptions explicitly thrown by throw expressions (plus, as Pavel noted, some standard C++ exceptions thrown intrinsically by standard operator new , dynamic_cast etc).

What is a null pointer in C?

A null pointer has a reserved value that is called a null pointer constant for indicating that the pointer does not point to any valid object or function. You can use null pointers in the following cases: Initialize pointers. Represent conditions such as the end of a list of unknown length.

Does C have null?

The C and C++ languages have a null character (NUL), a null pointer (NULL), and a null statement (just a semicolon (;)). The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object.

What is NullPointerException C#?

A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type. In the following example, names is declared but never instantiated: C# Copy.


2 Answers

Dereferencing a NULL pointer is undefined behaviour in C++ - which means the code can appear to work. An exception isn't guaranteed to be thrown. You can use the

std::invalid_argument

exception (provide a meaningful value to it - "p is NULL"), but you'll have to do the check yourself.

like image 158
Luchian Grigore Avatar answered Sep 17 '22 00:09

Luchian Grigore


Usually, in C++ (or C for that matter), you never dereference a NULL pointer. Doing this has undefined behavior (likely a segfault on any implementation I know of, but anything could happen according to the standard). It's probably a bad thing in other languages as well, but I don't know those enough to assert that.

It's best to prevent the situation than to try to recover from it (which can't be done in C or C++ anyway).

The usual pattern to prevent some related programmer errors is to use assert() inside function bodies such as:

int foo(int* myint) {   // Ensure myint is not NULL   assert(myint);    // Do something with myint   (*myint)++;    return *myint; } 

Such assert() calls are completely ignored on release builds and thus have no cost in production. They just help the development. On debug builds, and if the condition is not met, the program aborts immediately with a very explicit error message. Running it through a debugger, you can easily check the call stack to investigate for the exact reason.

like image 22
ereOn Avatar answered Sep 17 '22 00:09

ereOn