By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,810 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,810 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<intexample
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

Feb 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
da*************@gmail.com wrote:
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<intexample
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 9 '07 #2

P: n/a
On 9 Feb 2007 04:59:24 -0800 in comp.lang.c++,
"da*************@gmail.com" <da*************@gmail.comwrote,
>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!

Feb 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.