Skip to content
Related Articles

Related Articles

C++ | new and delete | Question 5
  • Difficulty Level : Basic
  • Last Updated : 22 Sep, 2013

Is it fine to call delete twice for a pointer?




#include<iostream>
using namespace std;
  
int main()
{
    int *ptr = new int;
    delete ptr;
    delete ptr;
    return 0;
}

(A) Yes
(B) No


Answer: (B)

Explanation: It is undefined behavior to call delete twice on a pointer.

Anything can happen, the program may crash or produce nothing.


Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :