New operator with vectors

These questions are relatively straightforward. When using vectors, should the new operator be used when a new element is clicked? And which release method should I call? Here is what I mean:

 // Release method: 1. void ReleaseMethodOne( vector< int * > &ThisVector ) { // Clear out the vector. ThisVector.clear( ); return; } // Release method: 2. void ReleaseMethodTwo( vector< int * > &ThisVector ) { // Clear out the vector. for( unsigned uIndex( 0 ); uIndex < ThisVector.size( ); uIndex++ ) { delete ThisVector.at( uIndex ); } return; } int main( ) { vector< int * > Vector; // Add a new element. Vector.push_back( new int( 2 ) ); // More code... // Free the elements before exiting. Which method should I call here? ReleaseMethodOne( Vector ); // This one? ReleaseMethodTwo( Vector ); // Or this one? return 0; } 

I started studying vectors recently, and the book I studied said that the vector clear( ) method calls each of the element destructors. Does this apply to the new operator?

+4
source share
6 answers

STL containers store copies of the objects you specify, pointers in your example. They never free the memory that you explicitly allocated. You must free this memory yourself, so you must use the second release method.

Of course, you do not need new every int . Just use vector<int> instead - you don’t have to deal with manual memory management at all.

+5
source

The clear method will actually call destructors. However, your vector stores pointers, and the destructor for pointers is trivial no-op. It does not call delete .

Therefore, simply calling clear will not free up memory for all int objects that you allocated with new . You need to delete them.

If you use a smart pointer instead of a regular pointer, then objects with a pointer will be freed at the appropriate time without having to do anything special.

+5
source

You must use ReleaseMethodTwo() , because if you allocated memory, then it is your responsibility to delete it.

std::vector::clear() only erases elements from the vector. It does not cause delete items to be deleted!

+2
source

For what you do there, you will need to use ReleaseMethodTwo . What this means by saying that it calls the element’s destructor is that the classes it contains (not containing pointers) will lose their scope and their destructors will be called.

An STL container will never call delete for you, as far as I know. If you select and pass a pointer, you will need to free.

+2
source

Like others, a vector calls the destructor of each element. In your case, the elements are pointers and therefore do not free up memory. For your application, the best choice is to use an intelligent reference counting pointer such as boost: shared_ptr. When there are no more references to elements (for example, an example you wrote), the memory will be automatically freed.

PS: Do not use a smart pointer without a link, such as std :: auto_ptr with a vector.

+1
source

if you use vactor<int *> than you have a vector of pointers, and you must allocate memory for each element and free this memory yourself. It makes no sense to use a pointer vector if the size of the type you want to save in the vector is quite large.

In fact, when you do vector<T>::push_back(val) , it is going to save a copy of val using the copy constructor T::T(T &orig) and call the destructor for all elements when you do clear()

0
source

Source: https://habr.com/ru/post/1337065/


All Articles