FAQ 31.07 How should a pointer variable be handled after being passed to delete ?

FAQ 31.07 How should a pointer variable be handled after being passed to delete?

The pointer variable should immediately be put into a safe state.

After calling delete p, immediately set p = NULL or p = anotherAutoPtr (unless the pointer p is just about to go out of scope). The goal is to prevent a subsequent operation from following the pointer p (which now points at garbage) or calling delete p a second time.

Note that setting p = new Fred() is not acceptable, since the Fred allocation may throw an exception before p is changed to a safe state.

We recommend setting p = NULL immediately, in case an exception subverts the normal flow of control.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net