Which situation can lead to a double-delete error in C++?

Options

  • A. Deleting the same dynamically allocated object twice
  • B. Declaring two integer variables
  • C. Using a local variable inside a function
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Deleting the same dynamically allocated object twice

Detailed Explanation

Answer: Deleting the same dynamically allocated object twice

A double delete can occur when a program attempts to release the same dynamically allocated object more than once.

For example, executing delete p; and then attempting to execute delete p; again without making the pointer safe can result in undefined behavior.

Modern C++ smart pointers and proper ownership design can greatly reduce this type of error.