Hai friends,
Me too have some doubts regarding C++.
What happens if a pointer is deleted twice?
Which is a better option --pass by value or pass by reference ?

Recommended Answers

All 3 Replies

>>What happens if a pointer is deleted twice
Depends. If you set the value of the pointer to 0 after the first delete then nothing will happen. But if you don't then the program will probably crash.

>>value or pass by reference
Nothing -- almost. They both have the same idental affect on the original variable

suppose there is pointer var p.first time delete p is excuted ,object *p is safly destructed,and memory pointed by p is safely moved to heap.and if second time same pointer is passed to delete without any new,then again memory returs to by that pointer p is moved to heap,thus heap is corrupted and list of free memory.

commented: Don't bump old threads -1

to delete a pointer and know that it will never be a problem do

Foo* p = new Foo;
// later on
delete p;
p = 0;
// since p is zero calling delete again will be safe
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.