What happens when a null pointer is dereferenced in C++?

int *p = nullptr;
cout << *p;
                        

Options

  • A. It always prints 0.
  • B. It always prints the pointer address.
  • C. It results in undefined behavior.
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. It results in undefined behavior.

Detailed Explanation

Answer: (C) It results in undefined behavior.

nullptr represents a null pointer and does not point to a valid object.

Attempting to access the object through *p when p is null is invalid.

The C++ standard does not specify a particular result for such an operation. It is therefore undefined behavior.

It may appear to cause a crash on many systems, but a specific result should not be assumed.