2

If I have a fixed number of elements of class MyClass, should I use arrays or vectors?, ie:

MyClass* myArray[];

or

std::vector<MyClass*> myVector;

?

Tatanan
  • 207
  • 2
  • 10
  • 2
    Probably `std::array`. It depends on how many elements you have. If there are too many, you may have to use `std::vector`. – juanchopanza May 15 '14 at 10:17
  • 1
    If the elements are managed by the current instanceor in the current scope use `std::array` or `std::array` otherwise. – Pixelchemist May 15 '14 at 10:17
  • 2
    Also consider to use vectors of the class instead of vectors of pointers. A pointer has nothing to do there except you are doing polymorphism or sharing ownership, cases where smart pointers are far better than a naked C pointer. – Manu343726 May 15 '14 at 10:54
  • 2
    `vector` is much less headache than `vector`, if you can meet your requirements that way. (same goes for `array`) – M.M May 15 '14 at 11:04
  • Why the pointers in `std::vector` and `MyClass*[]`? – Shoe May 15 '14 at 11:06
  • 1
    If you must use pointers to `MyClass`, you should at least use `shared_ptr` or `unique_ptr`. – Rook May 15 '14 at 11:06
  • @Jefffrey I don't know. Maybe just because I have sent it somewhere. I use * every time a have public vars in my classes that are going to be accessed from another classes. For example, if my class Player has a vector of Pieces that is going to be accessed by Board, then I make Piece* a pointer. I understand, maybe incorrectely, the no-pointers as data local to the current scope, and I use pointers to "more important" less local data. – Tatanan May 15 '14 at 14:22
  • @MattMcNabb Why does * give more problems? I don't fully understand the difference. – Tatanan May 15 '14 at 14:23
  • 1
    @Tananan, a value of type `T *` says "over there, there is a `T`". If your vector is full of these , then you still have to create all the `T`'s somewhere else, so you can set your vector up to contain pointers to them. So now you not only have to manage your vector but you also have to manage where the `T`'s are being stored, and who is responsible for creating new ones and deleting old ones. This gets hairy if your code includes the possibility of handing out a pointer to another function to operate on. – M.M May 15 '14 at 21:04

3 Answers3

5

Use std::array or raw arrays for a small, static number of elements.

If you have a lot of elements (more than say 100kb), you hog the stack and are asking for stack corruption / overflow. In that case, or if the number of elements can only be known at runtime, use std::vector.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Which is the difference between std::array and raw arrays? Isn't std::array just the same as array but specifying the namespace? – Tatanan May 15 '14 at 14:17
  • 1
    That would probably make a good separate question. For starters though, there's http://en.cppreference.com/w/cpp/container/array – tenfour May 15 '14 at 14:28
3

if you know the number in compile time - use static array.

if the number is dynamic (obtained from the user) - vector is much better to save you the hurdle of managing the memory

NirMH
  • 4,769
  • 3
  • 44
  • 69
2

"Fixed" has two meanings in this context. The usual one is set once, never change, such as a value read from input. This value is known at runtime and requires dynamic allocation on the heap. Your options are a C-style array with new or a vector; it is highly recommended you use a vector.

#include <vector>
#include <iostream>
int main() {
    int size;
    std::cin >> size;
    int *myArray = new int[size];
    std::vector<int> myVector(size);
}   

"Fixed" can also mean a compile-time constant, meaning it is constant for any run of the program. You can use a C-style array or a C++ array (automatic memory allocation on the stack).

#include <array>
int main() {
    const int size = 50;
    int myArray[size];
    std::array<int, size> myArray;
}

These are faster, but your program needs to have access to sufficient stack memory, which is something you can change in your project settings. See this topic for more info. If the size of the array is really big, you may want to consider allocating on the Heap anyway (vector).

Community
  • 1
  • 1
Gaminic
  • 581
  • 3
  • 9