Destructor - should I use delete or delete []?

I am writing a template class that takes a pointer as a pointer and saves it. A pointer is intended to point to an object allocated by another class, and passed to this containing class.

Now I want to create a destructor for this container. How do I free the memory pointed to by this pointer? I do not know a priori, whether it be an array or a single element.

I'm kind of new to C ++, so bear with me. I always used C, and Java is my OO choice language, but between the desire to learn C ++ and the speed requirements of my project, I went with C ++.

Would it be better to change the container from a template to a container for an abstract class that can implement its own destructor?

+7
c ++ destructor templates
source share
11 answers

If you do not know if it was allocated using new or new[] , then deleting it is not safe.

Maybe your code is working. For example, on the one platform I'm working on, the difference only matters when you have an array of objects with destructors. So you do this:

 // by luck, this works on my preferred platform // don't do this - just an example of why your code seems to work int *ints = new int[20]; delete ints; 

but then you do the following:

 // crashes on my platform std::string *strings = new std::string[10]; delete strings; 
+6
source share

You should document how this class expects to be used, and always allocate as expected. You can also pass a flag to an object, specifying how it should be destroyed. Also look at boost smart pointers that can handle this difference for you.

+6
source share

Short answer:

If you use [] with a new one, you want to use [] with deletion.

 //allocate some memory myObject* m = new myObject[100]; //later on...destructor... delete m; //wrong delete[] m; //correct 

These were bare bones, another thing you might pay attention to is boost . It is also quite difficult to answer, considering that you are not sure whether its array or a separate object. You can check this, though through a flag telling your application whether to use delete or delete [].

+5
source share

As a general rule of development, you should stick to a design in which the class that calls new must also call delete

+2
source share

You should not delete it at all. If your class accepts an already initialized pointer, deleting it is not safe. It may not even point to an object on the heap; calling delete or delete[] can be catastrophic.

The allocation and release of memory should occur in the same area. Which always owns and initializes an instance of your class is also supposedly responsible for initializing and passing the pointer, and that is where your delete should be.

+2
source share
  • Use delete if you allocated new .
  • Use delete[] if you selected new[] .

After these statements, if you still have a problem (maybe you want to delete an object that was created by someone else), you break the third rule:

  • Always delete what you have created. Consequence, never delete what you have not created.
+2
source share

(Moving my comment in response to the request.)

JonH's answer is right (about using array destruction only when using array construction), so maybe you should offer patterns: one for arrays and the other not.

Another answer is to avoid arrays and instead expect one instance, which may or may not be a proper collection that clears after itself, such as vector <>.

change

Sneaking from Roger Pate, I will add that you may need to use a smart pointer, which is a collection with one element.

+2
source share

If you have a class that takes a pointer, which it takes over, then the contract for using the class must include one of two things. Or:

  • the interface must indicate how the object that the pointer points to is pointed out, so the new owner may know how to safely free the object. The advantage of this parameter is that all things are simple (at the same level in any case), but they are not flexible - the class cannot handle the ownership of static objects, as well as dynamically allocated objects.

or

  • the interface should include a mechanism in which the deletion policy can be specified by giving a pointer to the class. It can be as simple as providing a mechanism for passing in a functor (or even a plain old function pointer) that will be called to free an object (preferably in the same function / constructor that passes in the pointer itself). This makes the class perhaps more difficult to use (but, for example, when specifying the delete pointer on the default pointer, the pointer can make it as easy to use as option 1 for most applications). Now, if someone wants to give the class a pointer to a statically allocated object, it can pass the no-op functor, so nothing happens when the class wants to free it, or the delete[] functor if the object was allocated new[] and t .d.
+2
source share

Since the C ++ pointer does not tell us how it was allocated, yes, there is no way to decide which reversal method to use. The solution is to give the user a choice that, we hope, knows how the memory was allocated. Take a look at Boost smart ptr , especially the shared_ptr constructor with the second parameter, for a great example.

+1
source share

A smart pointer like boost shared_pointer has already covered this, could you use it? linky

0
source share

Simply put, given only a pointer to dynamically allocated memory, there is no way to determine how to properly allocate it. The pointer could be highlighted in one of the following ways:

  • using new
  • using the new []
  • Using malloc
  • using custom function
  • and etc.

In all cases, before you can free memory, you must know how it was allocated.

0
source share

All Articles