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?
You need to call
deletebefore 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 usedeletelater on the same pointer. Never use one without the other.Also,
new []anddelete []go together in the same way, but you should never mixnew []withdeleteornewwithdelete []. 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 []).