Preventing delete same pointer twice

Apr 26, 2010 at 6:55pm
Hi,
if I want to prevent the delete the same pointer. could you please suggest solution

int main()
{
int *p,*q;
p=new int;
q=p;
delete q;
delete p; //can we have modification in this program we can implement prevent the the pointer twice

}
Apr 26, 2010 at 7:05pm
You need to check those things yourself or use come class wrapper for the pointer
Apr 26, 2010 at 7:11pm
Probably the best approach is to use a boost::shared_ptr and never need to explicitly delete it.

There might be some other work-arounds for the problem (such as setting a deleted pointer to zero, so that it may be deleted with no ill effects) but they are not fixing the real problem.

Also, consider a third party program, such as valgrind, to help debug memory issues.
Last edited on Apr 26, 2010 at 7:12pm
Apr 26, 2010 at 9:08pm
+1 for boost.

Apr 27, 2010 at 3:39pm
I am agree with all above option but it is possible by the program to prevent delete twice or
it is effect become null
Apr 27, 2010 at 4:52pm
closed account (1yR4jE8b)
+1000 for Boost

There's a reason parts of it are being adopted into the new standard...it effing rules!!!

But like Bazzy said, getting into the habit of setting deleted pointers to null is very beneficial, as deleting null has no effect.
Topic archived. No new replies allowed.