0

Suppose i have physics engine which need to solve systems of linear equation size of which is unknown in forward, but after i know it, i will not change it. One such matrix may easily take hundreds of kilobytes. The problem with vector is that i never really know how much space it has allocated and i don't want to allocate more than needed.

Most discussion tell us to use std::vector, but should i use std::unique_ptr<T[]> instead? Hm... maybe i need to use std allocator as this answer suggests?


From the standard:

After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise.

So it is not the option for me to use reserve.

I also found that there was proposition for dynarray which should have handled my case, and for now that is the way i'm going to take, if no other propositions will come.

Community
  • 1
  • 1
Yola
  • 18,496
  • 11
  • 65
  • 106
  • `The problem with vector is that i never really know how much space it has allocated` you can always check that with `capacity()`, can't you? Also since c++11 there is a `shrink_to_fit` function in the vector, though IIRC it is implementation-defined whether it will actually reduce the `size()` to the `capacity()` – SingerOfTheFall Apr 03 '17 at 11:24
  • @SingerOfTheFall u r right, i can tell this, but if it has allocated more than needed, to whom should i complain:) – Yola Apr 03 '17 at 11:28
  • I ended up coding various types of array encapsulator myself - everything is guaranteed (by me!). I still don't feel sad about it. Occurrence of `std::vector` in my program = 0. – javaLover Apr 03 '17 at 11:29
  • Quoting you "but if it has allocated more than needed, to whom should i complain". So let me get this straight. You want dynamic memory with no dynamic allocation? Of course it may allocate more than you need. If you don't want that, then don't use `push_back`, and just use `reserve` and `resize`. Let me make it clear: There's absolutely no advantage of using `unique_ptr` over vectors. Use the member functions `std::vector` to make its size suitable for your problem. – The Quantum Physicist Apr 03 '17 at 11:58

1 Answers1

2

From what you have said in your question, I would say that usage of std::vector should be fine: as soon as you know the size, you can reserve it in your vector(s) and there will ne no additional overhead because no allocation will never happen again in that part.

Of course, as a vector does allocate a raw array under the hood, you can also allocate it as soon as the size if known and deallocate it when done, but the memory gain should be limited to size of the vector struct itself, at least for 1-D arrays.

If you have huge multi-dimensional arrays, the gain will be higher because you will have on vector struct per row (and per plane in 3-D array), so using raw arrays instead of vector of size nn will be nsizeof(vector)

But it is a low level optimization, so you should only worry for that after all higher level optimizations have been exhausted (storing only one half for symetric matrixes for example).

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Hey, i've updated the answer. The problem is that when i ask for 1Mb vetor could allocate1.5Mb and that's a problem for me. – Yola Apr 03 '17 at 13:28
  • I know no implementation that would allocate 1.5Mb when asked 1Mb, and reserve was created specifically for that use. The allocation is said to be *greater or equal* because it generally exceeds the required size by several bytes to reserve some place for alignement problems (allocated memory is required to have an alignment suitable for any object type) and housekeeping information (guard bytes for valgrind for example). But AFAIK the overhead should not exceed 256 bytes and is generally much less. – Serge Ballesta Apr 03 '17 at 13:36
  • i checked wiht VC++ implementation and you are right. Thanks a lot! – Yola Apr 03 '17 at 13:56
  • Well, could you have a look at [WICTextureLoader.h](https://github.com/Microsoft/DirectXTK12/blob/master/Inc/WICTextureLoader.h) [Chuck Walbourn](http://stackoverflow.com/users/3780494/chuck-walbourn) uses `unique_ptr`, why? – Yola Apr 11 '17 at 10:14