Sobes.tech
Junior

What happens if you call the delete operator on nullptr?

sobes.tech AI

Answer from AI

Nothing will happen. Calling the delete operator on nullptr does not lead to an error or undefined behavior. The C++ standard explicitly guarantees this.

int* p = nullptr;
delete p; // Safe, nothing happens.

int* q = new int(10);
delete q; // Frees the allocated memory.
delete q; // Undefined behavior (double delete).

int* r = new int[5];
delete[] r; // Frees the array.