Seg fault!: passing vector<vector<int> > to a function

  • Thread starter danielhdez14142
  • Start date
D

danielhdez14142

Some time ago, I had a segment of code like

vector<vector<int> > example;
f(example);

and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector<int> > example;
example.push_back(vector<int>());
f(example);

I also remember that when debugging, I got the segmentation fault
right at the function call. I wonder why is it that I needed to do
this when the original code works if I had had a

vector<int> example
f(example)

and just pushed back integers inside f

I'm sorry because I don't have the actual code, this is a question
I'va had for a long time now and I simply don't have the code, but the
question is, can I pass vectors of vectors to a function without
initialising them and why if the answer is no?
Thanks

Daniel
 
V

Victor Bazarov

Some time ago, I had a segment of code like

vector<vector<int> > example;
f(example);

and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector<int> > example;
example.push_back(vector<int>());
f(example);

There is no reason for this code to have undefined behaviour. The
usual UB cause is the attempted use of non-existent vector contents.
If you pushed an empty vector, that empty vector does not acquire
any 'int' elements until you push them too.
I also remember that when debugging, I got the segmentation fault
right at the function call.

Well, my memory is also known to fail me now and then. No need to
worry.
I wonder why is it that I needed to do
this when the original code works if I had had a

vector<int> example
f(example)

and just pushed back integers inside f

I'm sorry because I don't have the actual code, this is a question
I'va had for a long time now and I simply don't have the code, but the
question is, can I pass vectors of vectors to a function without
initialising them and why if the answer is no?

You _can_ pass vectors. They are not "without initialising". They
are *default-initialised*. They don't contain any elements, that's
true. So any attempted use of operator[] will most likely fail.

V
 
D

David Harmon

On 9 Feb 2007 04:59:24 -0800 in comp.lang.c++,
and inside f, I defined vector<int>'s and used push_back to get them
inside example. I got a segmentation fault which I resolved by doing

vector<vector<int> > example;
example.push_back(vector<int>());
f(example);

Inside f() you probably do the equivalent of
example[0].push_back(42);

This fails if example[0] hasn't been created!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top