#Difference between STL Arrays and C arrays
48 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
std::array<T, N> is a convenience wrapper around a C style array of type T _private[N]
it prevents you from footgunning yourself.
Perfer it in all cases as it is just superior
This is the implementation by g++'s libstdc++
its a little annoying to read so I recommend putting it into an ide and using auto highlight
however its only like 300 lines long and is definetly understdable
C arrays decay easily when passed to functions (it can be prevented though) whereas std::array does not.
There are also certain constructs in modern C++ that cannot be applied to (decayed) plain C arrays, for example ranged-for (*).
So I am 100% with @desert vault ; prefer std::array over C arrays whenever/wherever possible.
example of reading out of bounds
this can get real dicey if you are using packed structures. for example reading at arr[4] on a C compiler would gladly return 88
and for these reasons, when passing std::array to some function, pass it as const std::array<T,N>& arr otherwise you will deep copy each element
which one has better performance tho?
they're the same
idk how it looks in assembly but id assume the array class takes more compiling time or something since it comes in a package
sure, but the runtime performance will be identical
compilation time takes longer because it's a template*
but that means nothing about its runtime performance
imho the safety and usability benefits it gives makes it worth regardless
there's a big deal breaker with std::array
you can't do this with std::array 
index[arr]
I think I saw stroustrup say in his site that he tried really hard to replicate the behavior 
wasn't possible in the end though
Lol
The difference is literal nano seconds.
At compile time
i doubt that very much ^^
there are a few joke "interviews with bjarne" and stuff like that out there.
tend to be published on april 1 😉
oooh TIL
maybe it was one of those
probably
i wonder if array template would have even faster runtime performance than c style array since the compiler will know exactly what you wanna do with the array and just write better machine code than it could’ve if you wrote something with a c style array
how is that different from a c style array?
it still knows the exact type
eh nevermind im just overthinking something like usual
std::array's fields are one fixed length array T arr[N], so no, you can't get std::array to beat C arrays in performance
Also you can’t change the size of a std::array while you could reallocate a C style array
Or can you call reallocate an std::array ?(sounds highly illegal regardless)
a dynamically allocated C-"array" is more akin to a std::vector anyway
a std::array is akin to a stack-allocated C-array, ie T[N]
std::unique_ptr<T[]>:
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.