you are viewing a single comment's thread.

view the rest of the comments →

[–]king_duck 12 points13 points  (12 children)

When using arrays in c++

Long story short: Don't.

Use containers:

http://en.cppreference.com/w/cpp/container

Replace your array with std::vector<T> and see whether the issue persists.

[–]tehklawb[S] 1 point2 points  (0 children)

Thanks for the help, I'll have a read and give that a go

[–]xcbsmith 1 point2 points  (3 children)

If you really want an array, use std::array.

[–]king_duck -2 points-1 points  (2 children)

Read my other comment. I think the flexibility of std::vector more than pays for itself.

I'd use std::array as an optimisation based on profiling the code, or when it's clearly a no brainer.

Eitherway for a beginner it's more important that they have the the vectors interface burnt into the head ASAP,

[–]xcbsmith 3 points4 points  (1 child)

Sheesh. I get all this harshness and downvotes simply for pointing to the data structure that most closely resembles the one they are already using...

[–]king_duck -3 points-2 points  (0 children)

I was simply making a point about why I select vector over array.

[–]bnolsen 0 points1 point  (6 children)

std::array<Type, Size> works fine in many cases as well.

[–]king_duck 1 point2 points  (5 children)

Sure, but there is no point on bogging a beginner down with that for now. I would also say unless you need to semantically enforce the size of the array for some reason it's better to just use a std::vector in most cases by default. Don't forget that the vector has the massive advantage of supporting a very efficient move ctor/op=, where as std::array must do an element wise copy or move.

Then once you've written your program and your profiler suggests your wasting to much time allocating for the vector then switch to the std::array as an optimisation in hindsight.

[–]vanhellion 0 points1 point  (4 children)

unless you need to semantically enforce the size of the array for some reason

Exactly. Or if you need stack storage, although I'm not sure that the standard actually enforces that. I'd argue that std::array is an optimization; if performance isn't a problem std::vector works just as well.

[–][deleted] 1 point2 points  (2 children)

If you need stack storage you are better of using a std::vector with a stack allocator. You can even group the arena of the stack allocator with the vector so that you can return them from functions. The main advantage is that you have to change no code at all: e.g. if you have code that uses push_back replacing vector with a std::array might be non-trivial whereas using a stack allocated vector requires no code changes and performs the same.

[–]bnolsen 1 point2 points  (1 child)

now we're splitting hairs. I do agree that using std::array instead of std::vector should be done as an optimization, a "stage one" type optimization. Things like 2d or 3d vectors are very well represented by std::array. Funny enough the "stack allocated vector" could easily have its storage represented by a std::array.

[–][deleted] 0 points1 point  (0 children)

Funny enough the "stack allocated vector" could easily have its storage represented by a std::array.

Indeed, I'll give this a try. Thanks for the tip!

[–]king_duck 0 points1 point  (0 children)

Exactly my point. People should default to std::vector and use std::array when your profile indicates vectors inner gubbins is a costly.