Stepperz96
Stepperz96

Reputation: 23

C++ How to create a dynamic array of vectors?

I'm having problem initialising an array of std::vectors.

I'm declaring and initialising it like this:

vector<component_change*>* _changes;
_changes = new vector<component_change*> [numThreads];

in the hope that it's in the same form as this:

int * foo;
foo = new int [5];

but when I hit a breakpoint after the initialisation, _changes' size is 0. What am I doing wrong and how can I fix it?

I don't want to use a vector of vectors as the number I need remains constant throughout the program but depends on the current hardware. And I'm not just looking for a single vector (Each vector will be used by a different thread then merged when the threads have finished their tasks).

Thanks guys! :)

Upvotes: 1

Views: 6809

Answers (5)

Neil Kirk
Neil Kirk

Reputation: 21813

new vector is usually wrong.

You should use, with most preferred if possible first,

std::vector<component_change> _changes(numThreads);

or

std::vector<std::unique_ptr<component_change>> _changes(numThreads);

or

std::vector<component_change*> _changes(numThreads);

or if each element of the vector should itself contain an array of components (it's not clear in your question)

std::vector<std::vector<**component_change**>> _changes(numThreads);

Declaring the component as one of the above ways, depending on your needs.

Note that the pointers begin not pointing to anything. You'd have to allocate the individual components as a separate step.

The following creates an array of numThreads vectors, not a vector of numThread elements.

new vector<component_change*> [numThreads]

Upvotes: 0

AbdulRahman AlHamali
AbdulRahman AlHamali

Reputation: 1941

I think you're kind of mislead, this size that you are reading belongs to the vector in the first element of the array. Its size is equal to 0 since no elements have been inserted in the vector yet.

Upvotes: 0

Databyte
Databyte

Reputation: 1481

Your program is correct. But you misinterpreted the debugger. _changes's size is not 0, but the first vector in your array (the one _changes points at) is empty. Thats because the debugger does not know if _changes points at a single element or an array (in that case the compiler would not know how many elements are in that array). Simply use a vector and call std::vector::shrink_to_fit.

Upvotes: 1

Mark
Mark

Reputation: 981

Are you interested in have a vector for each thread, or a vector containing items used by each thread? I assumed the later, but my answer could be adapted.

This is using a statically sized array; (this syntax is close).

const int NUMBER_OF_THREADS = 5;
component_change* _changes[NUMBER_OF_THREADS] =
{
  new component_change(1),
  new component_change(2),
  new component_change(3),
  new component_change(4),
  new component_change(5)
}

If the number of threads is dynamic, you will have to use a new...

int NUMBER_OF_THREADS = system.getThreadCount();
component_change* _changes = new component_change[NUMBER_OF_THREADS];

for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
  _changes[i] = new component_change(i+1);
}

If you want to a std::vector:

 int NUMBER_OF_THREADS = system.getThreadCount();
std::vector<component_change*> _changes;
_changes.reserve(NUMBER_OF_THREADS);

for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
  _changes.push_back(new component_change(i+1));
}

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 181068

If the size can be determined at compile time use a std::array. If the size is a run-time argument then use a vector and don't change the size of the container.

Upvotes: 0

Related Questions