What happens if you compile and execute this code?
class A {};

void Destroy(A *&ptr) {
    delete ptr;
    ptr = 0;
}

int main() {
    A *a = new A();
    Destroy(a);
    Destroy(a);
}
Explanation
In Destroy(), a reference to the pointer is passed, then after the first call of this function, the pointer a will become zero. During the second call of Destroy(), a null pointer will be passed to delete. By standard, the delete operator must correctly process the null pointer, so the program will exit successfully. Also, according to the standard, the main function may not accept arguments and may not contain a return statement.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes