"Thomas" <
XXXX@XXXXX.COM >writes:
Quote
So if I understood correctly, even if I have a multidimensional array of
pointers like "int ***p"
I just use "delete[] p;" correct?
If you allocated the internal arrays in your multidimensional array,
you have to delete each of them first. C++ pointer management is not
high level. For every allocation you need the correct corresponding
deallocation.
Skip some and you leak your objects. Delete incorrectly and you
corrupt your program and likely crash.
Be careful about mixing C and C++ allocations. Given a pointer out of
context, it's impossible to know how it was allocated and therefore
impossible to correctly determine how to deallocate it.
If it was allocated with C's malloc or calloc, you must call free. If
it was allocated with new, use delete. If it was allocated with
new[], use delete[]. If it is the address of a stack object, a global
variable, or other object that wasn't allocated, it should not be
deallocated. Unfortunately, it's entirely up to you to do proper
bookkeeping to know how the pointer needs to be treated.
That's where smart pointers come in. They're "smart" in that they
hold the pointer and know how to properly destroy them, provided
they're used correctly. For example, auto_ptr can not hold arrays
(even though it compiles, it is undefined what happens, but it's not
good!) Smart pointers help but are no substitute for being meticulous
and careful in your design. Get sloppy with pointers and your program
will be a horrible mess of crashes and mystery. :)
--
Chris (TeamB);