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

Discussion in 'C++' started by danielhdez14142@gmail.com, Feb 9, 2007.

  1. Guest

    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
    , Feb 9, 2007
    #1
    1. Advertising

  2. 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<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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 9, 2007
    #2
    1. Advertising

  3. David Harmon Guest

    On 9 Feb 2007 04:59:24 -0800 in comp.lang.c++,
    "" <> wrote,
    >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!
    David Harmon, Feb 9, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    458
    Victor Bazarov
    Feb 27, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,195
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,612
    Old Wolf
    Jan 20, 2004
  4. AMT2K5

    Seg Fault within function (New to C)

    AMT2K5, Jul 17, 2005, in forum: C Programming
    Replies:
    12
    Views:
    432
    akarl
    Jul 19, 2005
  5. Piotr
    Replies:
    10
    Views:
    542
    Daniel T.
    Jan 30, 2006
Loading...

Share This Page