3

According to the accepted answer on this question about raw arrays vs std::vector, the advantages of a raw array (back in 2010) were:

  • arrays are slightly more compact: the size is implicit
  • arrays are non-resizable; sometimes this is desireable
  • 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

To the best of my knowledge, std::array addresses all but the third point.

So unless I desperately need to improve my compile times, is there any reason to use a raw array over a std::array in C++11?

Ixrec
  • 966
  • 1
  • 7
  • 20
  • Can't see any advantage for raw arrays, it's just the other way round (I'm referring to `std::array` BTW not `std::vector`). – πάντα ῥεῖ Jan 12 '15 at 00:15
  • raw arrays can auto size a bit easier. – Yakk - Adam Nevraumont Jan 12 '15 at 00:23
  • 1
    @Yakk what do you mean, "auto size"? – Barry Jan 12 '15 at 00:26
  • 1
    @Barry `int a[] = {1, 2, 3};` – T.C. Jan 12 '15 at 00:26
  • 1
    If you have a function that expects an argument of type pointer/reference to array, then using `std::array` will require [an ugly cast](http://stackoverflow.com/questions/27749252/cast-boostarrayfloat-12-for-function-with-argument-const-float-arr12) – Praetorian Jan 12 '15 at 00:30
  • Most compilers support precompiled headers now, so header parse time isn't an issue – M.M Jan 12 '15 at 00:37
  • 1
    Related/duplicate: [Now that we have std::array what uses are left for C-style arrays?](http://stackoverflow.com/q/6111565) – dyp Jan 12 '15 at 00:51
  • raw arrays always have advantage in speed and memory – SSpoke Jan 12 '15 at 01:08
  • 2
    @SSpoke -1, why do you think `int a[10];` is larger than `std::array a`? – Yakk - Adam Nevraumont Jan 12 '15 at 01:30
  • @Yakk one of them is a primitive while the other is wrapped its pretty obvious – SSpoke Jan 12 '15 at 22:47
  • 4
    There is such a thing as a zero-cost abstraction. C++ is full of them. [This answer confirms that std::array has the same size as a raw array](https://stackoverflow.com/a/8778738/2727470). – Ixrec Jan 12 '15 at 23:04
  • 3
    @sspoke obvious, but wrong. `std::array` has no size overhead over an array. With a reasonable compiler, it has no runtime costs either. This is C++ not Java, things can exist in code without any runtime existance at all. – Yakk - Adam Nevraumont Jan 12 '15 at 23:08
  • @Yakk I do alot of reverse engineering and I notice anything you use from the library like std::blah it shows up in the strings. try out Hex-Rays decompiler and you'll see what I mean. – SSpoke Jan 12 '15 at 23:11
  • 1
    @SSpoke (A) on your compiler and (B) the existence of symbols/diagnostic, strings probably created by the exception handling for `.at()`, doesn't have any relevance to the size of the object itself, as you would know if you had analysed this properly. good grief – underscore_d Jan 28 '16 at 21:07

1 Answers1

3

Yes, it doesn't require you to explicitly specify the size, which makes it easier to initialize it by hand:

char const *messages[] =
{
    "Hi",
    "Bye",
    "foo",
    "bar"
};
user541686
  • 205,094
  • 128
  • 528
  • 886
  • Note that C++17 adds [CTAD](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction) to the core language and [`std::array` deduction guides](https://en.cppreference.com/w/cpp/container/array/deduction_guides) to the standard library, which allow to not explicitly specify the size *and* the type for the above code snippet. – Matthias Aug 14 '21 at 17:58