C++ | new and delete | Question 5

Is it fine to call delete twice for a pointer?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.