What can happen if a program accesses memory through a pointer after the pointed object has been destroyed?

Options

  • A. The access is guaranteed to return zero
  • B. The program has undefined behavior
  • C. The compiler always converts the pointer to nullptr
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. The program has undefined behavior

Detailed Explanation

Answer: The program has undefined behavior

After an object has been destroyed, a pointer that still refers to its former storage can become a dangling pointer.

Dereferencing such a pointer is not valid. The C++ standard does not define a predictable result for this operation, so the program has undefined behavior.