0

I am trying to use an array of vector like this:

vector<foo*> *vec = new vector<foo*>(n);

Some positions of vec will be empty and some of them will be later on filled with some data:

vec[i].push_back(&foo_var);

My problem is the empty positions of vec. Let's say that for i equal to 0, I did not push_back anything. Then, when I try to do some check, for example,

vec[0].size();

instead of the expected return 0, the program returns a huge number 1457283635.

Searching around, I found that I should had initialized the empty positions. Then I try this:

vector<foo*> vec = new vector<for*>(n);
for(int i = 0; i < n; i++) {
    vec[i] = vector<foo*>();
}
...

This "solution" was enough to make my program works as I expected (returning 0 as the size of an empty position, for instance).

However, after increasing the size of n, my program crashes in this line now:

vec[i] = vector<foo*>();

The program sends a segmentation fault error.

  • Can someone please explain what is happening?
  • How should I handle this specific case?
  • What are the best practices for vector initialization in this case?
9
  • new vector<for*>(n); you have a typo here (in both places where this line appears) Commented Mar 5, 2015 at 18:43
  • 1
    Why not initializing the "empty" slots with nullptr? Also I would seriously recommend using something like std::vector<std::vector<std::unique_ptr<foo>>>. Commented Mar 5, 2015 at 18:45
  • @Quest for is a keyword, it can't possibly appear as a typename in vector<for> Commented Mar 5, 2015 at 18:45
  • 1
    You're creating a single vector of size n instead of n vectors. Commented Mar 5, 2015 at 18:46
  • 5
    new vector 99% of the time means you are doing something wrong. Commented Mar 5, 2015 at 18:48

3 Answers 3

10

It seems you mean

vector<foo*> *vec = new vector<foo*>[n];

instead of

vector<foo*> *vec = new vector<foo*>(n);

The first one allocates indeed an array of vectors in the dynamic memory while the scond one allocates a single vector with n elements. Take into account that you could define a vector of vectors. For example

std::vector<std::vector<foo *>> vec( n );
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it seems that this is enough to avoid the segmentation fault error. Can you explain better why should I do a vector of vectors? As the people commented in my question, should I avoid doing an array of vectors as best practice?
@MFS Because it is the vector itself that will manage the allocated memory. You need not to bother about deleting or reallocating of the allocated memory.
0

Did you mean to create n empty vectors, or a single vetor filled with n elements?

I'm assuming the former but by using () in your new statement you've actually accomplished the latter, creating but a single vector with n foo* elements in it.

You should solve this by using a vector of vectors so you don't have to do memory management yourself.

std::vector<std::vector<foo*> > vec(n);

This then creates n empty vectors of foo*.

Comments

0

Do your requirements indicate you have to use dynamic memory?

If not, might be simpler to create as:

vector<foo*> vec;

Then use vec as a 'regular' vector variable, instead of using a pointer-to-vector?

Then it is simpler(?) to just do:

vector<foo*> vec;

foo* pFoo = something;

vec.push_back(pFoo);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.