0

I have a series of questions to ask.

  1. What is the correct way to destroy a vector of pointers? I know the vector class has the default destroyer but I do not know if the pointers change something.
  2. An array containing object pointers in its destruction requires the simple command: Delete [] pointname? Or do I have to do some other operation first on pointers?
  3. A list or vector of object pointers to be emptied requires a simple command: list_name / vector_name.clear (); Or do some other operations on the pointers first?
peterh
  • 11,875
  • 18
  • 85
  • 108
  • 3
    A single question per question please. Also, you provide not enough information. Sometimes you need to delete the pointers manually, sometimes you don't. – Rakete1111 Jul 21 '17 at 18:46
  • 1
    ***What is the correct way to destroy a vector of pointers?*** In modern c++ the best way is to not have a vector of pointers in the first place. Use a vector of smart pointers if you really need a pointer at all. – drescherjm Jul 21 '17 at 18:48
  • Learn and embrace [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and you won't have to worry about manually deleting anything. Let scope control all of that, the same way vector cleans up its underlying array, for example – Cory Kramer Jul 21 '17 at 18:50

2 Answers2

0

1) What is the correct way to destroy a vector of pointers? I know the vector class has the default destroyer but I do not know if the pointers change something.

If you have manually allocated the pointers in the vector and you want to delete (deallocate and destroy) them when the vector is destroyed, you should automate it with std::unique_ptrs

{ 
    std::vector<std::unique_ptr<int>> vec;
    vec.push_back(std::make_unique<int>(1));
    vec.push_back(std::make_unique<int>(2));
} 

when the vector is destroyed, the integers will be destroyed as well. If you are curious as to how this works and are not familiar with code like this, read up on RAII

2) An array containing object pointers in its destruction requires the simple command: Delete [] pointname? Or do I have to do some other operation first on pointers?

If you have allocated that array with new[] then you can call delete[] on that array. Keep in mind that this will not call delete on the integers individually, it will just deallocate the memory allocated for the array. If you want to deallocate the integers as well use unique_ptr as in the above example

3) A list or vector of object pointers to be emptied requires a simple command: list_name / vector_name.clear (); Or do some other operations on the pointers first?

This will clear the container but not free the pointers. See the above two points.

Curious
  • 20,870
  • 8
  • 61
  • 146
0

In answer to your second question, using delete[] and then the name of your array of pointers is satisfactory with no additional steps. delete[] just takes the array of pointers and causes the kernel to 'realise' the memory locations they point to so other operations can 'use' (write-over the data previously in) those locations. This is all you need to do. Now other operations can request memory and the kernel is free to 'give away' those locations.

Seal2434
  • 1
  • 2