What happens if the following C++ statement is compiled and executed? int *ptr = NULL; delete ptr;

Options

  • A. The program is not semantically correct
  • B. The program is compiled and executed successfully
  • C. The program is compiled successfully but throws an error during runtime
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. The program is compiled and executed successfully

Detailed Explanation

Answer: The program is compiled and executed successfully

In C++, applying delete to a null pointer is valid. It does not cause an error and no memory is released because the pointer does not point to an allocated object.

Therefore, the following is valid:

int *ptr = NULL;
delete ptr;

The program can compile and execute successfully.