Array of structures and new / delete

I have a structure like this:

class Items { private: struct item { unsigned int a, b, c; }; item* items[MAX_ITEMS]; } 

Say I wanted to “delete” an element, for example:

 items[5] = NULL; 

And I created a new element in the same place later:

 items[5] = new item; 

Do I still need to call delete[] to clear this? Or is it not needed, since the boundaries of the items[] array are known before compilation?

Does this pointer set to NULL valid or should I delete it?

+7
c ++ memory-management arrays new-operator struct
Jan 12 '09 at 0:53
source share
8 answers

You need to call delete before setting it to NULL. (Setting it to NULL is not required, it just helps reduce the number of errors if you accidentally try to play the pointer after deleting it.)

Remember that every time you use new , you will need to use delete later on the same pointer. Never use one without the other.

In addition, new [] and delete [] go together the same way, but you should never mix new [] with delete or new with delete [] . In your example, since you created an object with new (and not new [] that would create an array of objects), you should delete the object with delete (and not delete [] ).

+15
Jan 12 '09 at 1:04
source share

As Kluge noted, you will skip the object at index 5 like this. But it really sounds like you shouldn't do it manually, but use the container class inside the Item . If you really do not need to store these Item objects as pointers, use std::vector<item> instead of this array of MAX_ITEMS pointers. You can always insert or erase vector elements in the middle if you need to.

If you need to save objects as pointers (usually if the struct Item is actually polymorphic, unlike your example), you can use boost :: ptr_vector <item> from Boost.PtrContainer instead.

Example:

 class Items { private: struct item { unsigned int a, b, c; }; std::vector<item> items; } if (items.size() > 5) // (just to ensure there is an element at that position) items.erase(items.begin() + 5); // no need to use operator delete at all 
+6
Jan 12 '09 at 1:13
source share

To remove an item, use:

delete items [5];

after deleting an element, it is recommended to set the remote pointer to NULL, so you will not have an error if you later delete it by mistake.

items [5] = NULL

+1
Jan 12 '09 at 1:14
source share

Say I wanted to “delete” an element, for example:

elements [5] = NULL;

I don't know much about Visual Basic, but it smells like the Visual Basic programming idiom, since "Set a = None" (or Null, I'm not sure) will delete the object that it points to (or, rather, decrease its reference count, for COM- objects).




As someone else noted, you should use either:

 delete items[5]; items[5] = newContent; 

or

 delete items[5]; items[5] = NULL; 

After delete[5] only possible use of the pointer stored in items[5] is causing problems. The worst part is that this can happen from the very beginning, and only start crashing when you allocate something else in the space previously used by *items[5] . These are the reasons that make C / C ++ programming “interesting,” that is, really annoying (even for those who love C, like me).

Writing only delete items[5]; preserves what might be a useless record, but it's a premature optimization.

+1
Jan 12 '09 at 1:50
source share

Just to be clear: you refer to the call to " delete[] ". I think you mean delete .

I mention this because C ++ has two separate operators: operator delete and operator delete[] . The latter is used to remove arrays of objects selected using operator new[] , and is not used in this case. You have an array of pointers to objects that you should have initialized with repeated calls to operator new , rather than a single call to operator new[] .

All I'm really trying to say: your use of delete[] is confusing and ambiguous; change it to delete .

+1
Jan 12 '09 at 8:07
source share

There are several related questions here:

  • According to the code you sent, the array itself is not allocated on the heap, unless it is a struct , so you do not need to delete[] array. If you created an array with new[] , you would have to delete[] it.
  • The above code does not talk about how objects pointed out from the array are allocated. If you select these objects on the stack, you cannot delete them (again, this is unlikely because your pointers will become invalid when the objects they point to fall out of scope). If you allocated them to a bunch (with new ones), you should delete them when they fall out of the area.
  • As others have said, life is much easier if you use a container, especially an STL container, and smart pointers, which currently mean pointers to Boost.
+1
Jan 12 '09 at 8:20
source share

C ++ is not my strong suit, but I'm sure you will leak memory if you set the pointer to NULL .

EDIT: Leaked memory will be the memory pointed to by the pointer in the array.

0
Jan 12 '09 at 0:57
source share

Setting items [5] in NULL do not delete the memory associated with the item, it simply sets the pointer to this item to NULL, so a memory leak occurs.

You can remove an item by calling:

 delete items[5]; 

Since C ++ does not have automatic garbage collection, you need to delete any memory that you no longer need.

0
Jan 12 '09 at 0:59
source share



All Articles