Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a raw array have any advantages over a std::array? [duplicate]

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?

like image 817
Ixrec Avatar asked Jan 24 '26 00:01

Ixrec


1 Answers

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"
};
like image 133
user541686 Avatar answered Jan 26 '26 16:01

user541686