Preventing delete same pointer twice

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

}
You need to check those things yourself or use come class wrapper for the pointer
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
+1 for boost.

I am agree with all above option but it is possible by the program to prevent delete twice or
it is effect become null
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.