#Difference between STL Arrays and C arrays

48 messages · Page 1 of 1 (latest)

placid hawk
#

Is there a difference between how the following two arrays are implemented?
std::array<int,n> a
int a[n]
Which is better and why?

delicate burrowBOT
#

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.

desert vault
#

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

wooden tiger
desert vault
#

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

shell glade
spare mountain
#

which one has better performance tho?

pseudo delta
spare mountain
#

idk how it looks in assembly but id assume the array class takes more compiling time or something since it comes in a package

pseudo delta
#

sure, but the runtime performance will be identical

odd bane
opal karma
marsh fox
#

there's a big deal breaker with std::array

#

you can't do this with std::array nooo

index[arr]
crude apex
#

?

#

oh lol

#

thank god you can't 😛

marsh fox
#

I think I saw stroustrup say in his site that he tried really hard to replicate the behavior kekw

#

wasn't possible in the end though

desert vault
#

Lol

desert vault
#

At compile time

marsh fox
#

I'm gonna try to find it

#

probably sarcastic anyways

crude apex
#

there are a few joke "interviews with bjarne" and stuff like that out there.

#

tend to be published on april 1 😉

marsh fox
#

maybe it was one of those

crude apex
#

probably

spare mountain
#

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

opal karma
#

it still knows the exact type

spare mountain
#

eh nevermind im just overthinking something like usual

shell glade
little herald
#

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)

odd bane
#

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]

tranquil linden
#

std::unique_ptr<T[]>:

delicate burrowBOT
#

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.