0

I saw the following from this link:

Vectors are part of STL. Vectors in C++ are sequence containers representing arrays that can change their size during runtime . They use contiguous storage locations for their elements just as efficiently as in arrays, which means that their elements can also be accessed using offsets on regular pointers to its elements.

Vectors are the dynamic arrays that are used to store data.It is different from arrays which store sequential data and are static in nature, Vectors provide more flexibility to the program. Vectors can adjust their size automatically when an element is inserted or deleted from it.

If vectors can do so much, under what circumstances do we still prefer arrays?

Thanks!

SE_
  • 55
  • 7
  • There is no need to dynamically allocate memory if the size of an array is known at compile time and is not a big number – Vlad from Moscow Oct 24 '21 at 15:15
  • 1
    Arrays are slightly faster to create and destroy, and have slightly less (zero) memory overhead. – HolyBlackCat Oct 24 '21 at 15:16
  • Is the size of the container known at compile-time? Will it stay the same size throughout the life-time of the program? Is it okay that the container data might be allocated as a local variable (i.e. on the stack), depending on use-case? Then go for an array. – Some programmer dude Oct 24 '21 at 15:16
  • True... but in that case, would using a vector instead of an array hurt (from a memory or efficiency perspective)? – SE_ Oct 24 '21 at 15:17
  • 2
    @Erin -- Are you aware that `std::array` exists? Also, that link you have has this: `#include ` -- Don't learn C++ from that site. – PaulMcKenzie Oct 24 '21 at 15:26
  • 1
    @Erin: Yes, somewhat. The `vector` needs to `new` and `delete` the memory to store the objects, which takes a certain amount of time, and it also has internal data members to keep track of size, capacity, pointer to data, etc ([24 bytes here](https://godbolt.org/z/M3ss8xoWh)) which occupy memory beyond what's needed to store the objects. Raw arrays and `std::array` don't have any of that. – Nate Eldredge Oct 24 '21 at 15:27
  • To elaborate on the previous comment by @PaulMcKenzie: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Oct 24 '21 at 15:42
  • I still use "C" style arrays is if I write code I that needs to be evaluated at compile time (constexpr) and/or if the data size can be calculated at compile time. The biggest performance hit for std::vector is when it already takes up a lot of memory and needs to be reallocated (which you sometimes can prevent with a call to reserve early on). But as daily driver I use it a lot. – Pepijn Kramer Oct 24 '21 at 15:43
  • STL, that's a name I haven't heard in a long time. – Captain Giraffe Oct 24 '21 at 18:04

2 Answers2

4

If vectors can do so much, under what circumstances do we still prefer arrays?

A good design is not when there is nothing left to add, rather when there is nothing left to remove. Or introducing extra complexity only when it is needed.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

One major disadvantage of std::vector is that it uses dynamic memory allocation whenever it must grow, which can be quite expensive in terms of execution time. Therefore, in situations when a reasonable upper limit is known, it may be better to use a fixed-length array, even if this wastes memory space. This decision is a space/time trade-off.

However, this disadvantage of std::vector can be mitigated by reserving memory for a certain amount of elements in advance, by calling std::vector::reserve.

Another disadvantage of using std::vector is that serialization is significantly harder. For example, if you make a std::array a member of a struct, then you can simply copy the entire struct contents byte by byte in order to serialize it. However, this is not possible if you make a std::vector a member of a struct, as it will probably not store the actual data inside the struct, but will probably only store a pointer to the data. Therefore, you will be unable to serialize the struct by simply copying the memory of the struct. Instead, serializing the std::vector will require special treatment.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 6
    But if the container needs to grow, then an array wasn't an option in the first place. – Nate Eldredge Oct 24 '21 at 15:24
  • 1
    @NateEldredge: Depending on the situation, a reasonable upper limit may be known, and the array could be made to have that size. However, you are right that this will probably be a waste of space in many situations. Therefore, the decision whether to use a `std::vector` or a `std::array` is often a space/time trade-off. – Andreas Wenzel Oct 24 '21 at 15:32