1

Suppose I am using a vector of int a:

vector<int> a {1, 2, 34, 1222, 0};

Then, I want to push_back some datas like this:

a.push_back (data);

However, my program have to add a lot of data on my vector.

I am learning c++ on a book. The book says: "Adding an element can sometimes require additional storage be allocated. In that case, every element must be moved into the new storage", the c++ Ressources Network adding that "This is a relatively expensive task in terms of processing time.". I also find an article about the subject.

However, my program have to be very fast. So, I can not deal with this "relocation" of my vector. In fact the size of my vector can be up than 1GO of memory.


My question is quite simple:

How can I know the remaining memory before relocating my huge vector?


Note: I do not know the finally size of my vector so I can not initialize it keeping the necessary memory.

Tell me if you have some questions or some comments.

SphynxTech
  • 1,799
  • 2
  • 18
  • 38
  • 7
    *[std::vector::capacity](http://en.cppreference.com/w/cpp/container/vector/capacity) returns the number of elements that the container has currently allocated space for.* When you insert an element and `std::vector::capacity` equals to `std::vector::size` then reallocation happens. – songyuanyao Apr 19 '18 at 08:43
  • 3
    The remaining memory of your system or the remaining/reserved memory in the vector? – Simon Kraemer Apr 19 '18 at 08:43
  • 2
    You should not really worry about vector internals. It works quite good. Different containers have different complexities for different task. Choose the one that best fits your use case. – Ron Apr 19 '18 at 08:43
  • 1
    You *could* use `a.capacity` but I'm not sure what you are planning on doing with that information. – Chris Drew Apr 19 '18 at 08:44
  • 5
    This looks like an [XY Problem](http://xyproblem.info/). What problem are you _actually_ trying to resolve? – Jabberwocky Apr 19 '18 at 08:45
  • 2
    if that article didnt mention capacity of the vector or `vector::reserve` then it missed the point – 463035818_is_not_an_ai Apr 19 '18 at 08:45
  • @ChrisDrew OP could manually reserve additional memory to improve the performance for a specific use case. – Simon Kraemer Apr 19 '18 at 08:45
  • 1
    Take a look at https://stackoverflow.com/a/5232342/612920 – Mansuro Apr 19 '18 at 08:48
  • Look like you should consider using `std::deque`. – Richard Critten Apr 19 '18 at 08:49
  • 2
    @SimonKraemer I'm all for using `vector::reserve` if possible but I don't see how using `vector::capacity` helps in OPs case. – Chris Drew Apr 19 '18 at 08:49
  • "my program have to be very fast. So, I can not deal with this relocation". We call this **premature optimization**. Having said that, it would be a wise advice to stick to the _generic_ container interface (defined by `begin()/end()` iterators) so you can swap to other containers if necessary. – MSalters Apr 19 '18 at 12:24

2 Answers2

2

std::vector has methods for this case:

capacity() returns current number of elements, for which memory is allocated. So if the number of elements in vector would exceed this number, reallocation will happen

reserve() allocates memory for given number of elements. If given number is less than capacity(), it does nothing. Otherwise, it reallocates to provide the required capacity.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
2

If you knew how big your vector would be eventually then you could use std::vector::reserve to avoid any reallocations but you say that is not possible.

vector::push_back is guaranteed to be amortized constant. It achieves this by allocating more memory than it needs. As the vector gets larger there will be fewer and fewer reallocations. vector is designed for this so I suggest you try it and then measure performance to see if the reallocations are a problem.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • If, having measured, the performance is still problematic, switching to `std::deque` will be very little change, and is safe as long as you don't need a `ContiguousContainer` – Caleth Apr 19 '18 at 11:49
  • @Caleth Sure. There is no guarantee `std::deque` will improve performance though. `std::vector` will have better data locality. Allocations will be faster with `std::deque` but it will have to do more allocations in total if the container size is large. – Chris Drew Apr 19 '18 at 12:19