13

I'm currently seeing a lot of questions which are tagged C++ and are about handling arrays.
There even are questions which ask about methods/features for arrays which a std::vector would provide without any magic.

So I'm wondering why so much developers are chosing arrays over std::vector in C++?

MOnsDaR
  • 8,401
  • 8
  • 49
  • 70

5 Answers5

14

In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays:

  • Arrays are slightly more compact: the size is implicit.
  • Arrays are non-resizable; sometimes this is desirable.
  • Arrays don't require parsing extra STL headers (compile time).
  • It can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using).
  • Fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed.
Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
  • 2
    Also: * arrays are faster; * arrays can be initialized statically (while vectors are always initialized dynamically, at run time) – mojuba Oct 23 '10 at 12:55
  • 12
    Alas the reason that "so much developers are chosing arrays over std::vector in C++" is just because they read some old/bad books and now are reluctant to switch to a new level of programming. I doubt many even consider any of your points before deciding using arrays. Also no matter what vector provides, any programmer must theoretically be able to use arrays, because if you don't understand pointers and their arithmetic you can't be a good c++ programmer – Armen Tsirunyan Oct 23 '10 at 13:07
6

Because C++03 has no vector literals. Using arrays can sometime produce more succinct code.

Compared to array initialization:

char arr[4] = {'A', 'B', 'C', 'D'};

vector initialization can look somewhat verbose

std::vector<char> v;
v.push_back('A');
v.push_back('B');
...
Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
2

I'd go for std::array available in C++0x instead of plain arrays which can also be initialized like standard arrays with an initializer list

https://en.cppreference.com/w/cpp/container/array

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
David
  • 3,324
  • 2
  • 27
  • 31
1

You have much more control with arrays

How about:

1) you are dealing with colossal data sets where the data has to be mapped files and not allocated with malloc or new because of its size. Under this scenario worrying about what to do if you didn't reserve enough address space at the beginning may be moot, though I suppose you could unmap - extend - remap the file, unless prohibited by address fragmentation or my second point.

2) Code that uses lockless multiprocessing. Performance hits of stopping the threads for re-allocation (or any other "STL goodie") may be unacceptable, hence use arrays, you have got much more control, you may need to call a lot of functions to pause other threads before you resize anything.

BTW, I'm usually dealing with 1 and 2 at the same time. Arrays of structures + pointers work wonderfully, Compiling with C++ because you you can still use some C++ features elsewhere in the code. I'm sure I could think of many more examples if I tried hard enough

user1741137
  • 4,949
  • 2
  • 19
  • 28
camelccc
  • 2,847
  • 8
  • 26
  • 52
1

I think this is because a lot of C++ programmers come from C and don't yet understand the advantages of using vector and all the extra STL goodies that come for free with its containers.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140