[16.7] Do I need to check for NULL before delete p?
No!
The C++ language guarantees that delete p will do nothing if p is equal to
NULL. Since you might get the test backwards, and since most testing
methodologies force you to explicitly test every branch point, you should
not put in the redundant if test.
[16.10] How do I allocate / unallocate an array of things?
Use p = new T[n] and delete[] p:
Fred* p = new Fred[100];
// ...
delete[] p;
Any time you allocate an array of objects via new (usually with the [n] in
the new expression), you must use [] in the delete statement. This syntax is
necessary because there is no syntactic difference between a pointer to a
thing and a pointer to an array of things (something we inherited from C).
|