What will happen if the following C++ statement is compiled and executed? int…

20232023

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

  1. A.

    The program is not semantically correct

  2. B.

    The program is compiled and executed successfully

  3. C.

    The program gives a compile-time error

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 154 students.

Show answer & explanation

Correct answer: B

In C++, deleting a null pointer is a well-defined operation and does not cause a runtime error. The statement int *ptr = NULL; initializes a pointer to null, and delete ptr; is safe because the delete operator checks for null before attempting to deallocate memory. If the pointer is null, delete does nothing. Therefore, the program compiles and executes successfully without any errors.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs