Delete items (and resize) std :: vector without affecting allocated memory

I am using the following code:

const int MY_SIZE = 100000; std::vector<double> v; v.reserve(MY_SIZE); // add no more than MY_SIZE elements to the vector f(v); v.clear(); // again, add no more than MY_SIZE elements to the vector f(v); v.clear(); // // etc... // 

The point of my code is to save MY_SIZE double , and then execute the f(std::vector<double>) operation for these elements. After filling the vector and performing the operation, I want to get rid of all the elements (and reset std::vector::size() to 0), and then add a few more elements. But the key here is that I do not want the memory space allocated for the vector to be changed.

Note that I will never add more MY_SIZE elements to v , so v does not need to reallocate more memory than v.reserve(MY_SIZE) was allocated.

So, when I call v.clear() in the above code, will it in any way affect the amount of space allocated by v.reserve(MY_SIZE) , or the memory space of v.begin() ?


A related question : if I call v.erase(v.begin(),v.begin()+v.size()) , this will in some way affect the amount of space allocated by v.reserve(MY_SIZE) , or in place in v.begin() 's memory?

If I just wanted to erase all the elements, I would call clear() . But I'm curious about this related question, because there are times when I need to erase only the first X-elements of v , and in these cases I want to keep the allocated memory v.reserve(MY_SIZE) , and I don't want the location v to change .

+4
source share
1 answer

It seems that the C ++ (2003) standard implicitly guarantees that memory is not reallocated if you call the clear() or erase() method of std::vector .

In accordance with the requirements of the sequence (table 67) a.clear() equivalent to a.erase(begin(),end()) .

In addition, the standard states that the erase(...) member function of the std::vector<T> element does not throw an exception unless it is thrown by the copy constructor T (section 23.2.4.3). Therefore, this is implicitly guaranteed, since redistribution may cause an exception (sections 3.7.3, 20.4.1.1).

Also v.begin() remains unchanged, since erase(...) will invalidate all iterators after the delete point (section 23.2.4.3). However, this will not be dereferenced (since v.begin() == v.end() ).

So, if you have a standard compatible implementation, you're fine ...

CORRECTION

My reasoning is wrong. I managed to show that erase(...) does not redistribute, but the implementation can still free memory if you delete all the elements. However, if a.capacity() says β€œyou can add N elements without reallocating memory” after deleting / cleaning, you're fine.

The C ++ 11 standard defines a.clear() without reference to a.erase(...) . a.clear() not allowed to throw an exception. Consequently, it can be freed, but not redistributed. Therefore, you should check the capacity after cleaning the vector to make sure that the memory still exists and that the next resizing will not be redistributed.

+1
source

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


All Articles