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

Asked In: BPSC TRE 1.0

Options

  • A. The program is not semantically correct
  • B. The program is compiled and executed successfully
  • C. The program gives a compile-time error
  • 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

Correct Answer: Option B

Explanation: In C++, applying delete to a null pointer is valid and does nothing. Therefore, int *ptr = NULL; delete ptr; is valid, assuming the required declarations are available.