I have a struct like this:
class Items
{
private:
struct item
{
unsigned int a, b, c;
};
item* items[MAX_ITEMS];
}
Say I wanted to 'delete' an item, like so:
items[5] = NULL;
And I created a new item on that same spot later:
items[5] = new item;
Would I still need to call delete[]
to clean this up? Or won't this be needed since bounds of array items[]
are known before compiling?
Is setting that pointer to NULL valid or should I be calling delete there?
I know little Visual Basic, but that smells like a Visual Basic programming idiom, since "Set a = None" (or Null, I'm not sure) would delete the object pointed by a (or rather decrement its reference count, for COM objects).
As somebody else noted, you should use either:
or:
After
delete[5]
, the only possible use of the pointer stored initems[5]
is causing you trouble. What's worse is that it might happen to work at the beginning, and start failing only when you allocate something else over the space previously used by*items[5]
. Those are the causes which make C/C++ programming "interesting", i.e. really annoying (even for who likes C like me).Writing just
delete items[5];
saves what can be an useless write, but that's a premature optimization.Setting items[5] to NULL doesn't delete the memory associated with the item, it simply sets the pointer to that item to NULL, therefore the memory is leaked.
You can delete the item by calling:
Since C++ has not automatic garbage collection, you need to delete any memory you no longer need.
There are a few, related, questions here:
struct
is, so you don't need todelete[]
the array. If you created the array withnew[]
you would have todelete[]
it.As Kluge pointed out, you'd leak the object at index 5 like that. But this one really sounds like you shouldn't do this manually but use a container class inside
Item
. If you don't actually need to store theseitem
objects as pointers, usestd::vector<item>
instead of that array ofMAX_ITEMS
pointers. You can always insert or erase vector elements in the middle as well if you need to.In case you need to store the objects as pointers (usually if struct
item
is actually polymorphic, unlike in your example), you can use boost::ptr_vector<item> from Boost.PtrContainer instead.Example:
To delete an item use:
delete items[5];
after deleting the item it is advisable to set the deleted pointer to NULL, so you won't have an error if you later delete it again by mistake.
items[5] = NULL
You need to call
delete
before setting it to NULL. (Setting it to NULL isn't required, it just helps reduce bugs if you accidentally try to dereference the pointer after deleting it.)Remember that every time you use
new
, you will need to usedelete
later on the same pointer. Never use one without the other.Also,
new []
anddelete []
go together in the same way, but you should never mixnew []
withdelete
ornew
withdelete []
. In your example, since you created the object withnew
(rather thannew []
which would create an array of objects) you must delete the object withdelete
(rather thandelete []
).