segmentation fault

Discussion in 'C++' started by Chris, Aug 13, 2006.

  1. Chris

    Chris Guest

    I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
    It compiles fine, then when I run it it simply prints "segmentation
    fault".

    int main() {
    vector<vector<string> > yo;
    yo[0][0] = "hello";
    cout << yo[0][0];

    return 0; }

    1. What's causing this?

    I'm using the object "vector<vector<string> >" so I can split a string
    into lines, then split the lines into words so that
    "hello there\nTommy Boy"
    becomes
    [ ["hello", "there"], ["Tommy", "Boy"] ]

    2. Should I do this a different way?

    Thanks for any help!
    Chris, Aug 13, 2006
    #1
    1. Advertising

  2. Chris

    Ian Collins Guest

    Chris wrote:
    > I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
    > It compiles fine, then when I run it it simply prints "segmentation
    > fault".
    >
    > int main() {
    > vector<vector<string> > yo;
    > yo[0][0] = "hello";
    > cout << yo[0][0];
    >
    > return 0; }
    >
    > 1. What's causing this?
    >

    Surely this must be an FAQ by now, if not, look back though the past
    week or so of postings. You can't use operator [] on an empty vector.

    --
    Ian Collins.
    Ian Collins, Aug 13, 2006
    #2
    1. Advertising

  3. Chris

    Jim Langston Guest

    "Chris" <> wrote in message
    news:...
    > I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
    > It compiles fine, then when I run it it simply prints "segmentation
    > fault".
    >
    > int main() {
    > vector<vector<string> > yo;
    > yo[0][0] = "hello";
    > cout << yo[0][0];
    >
    > return 0; }
    >
    > 1. What's causing this?
    >
    > I'm using the object "vector<vector<string> >" so I can split a string
    > into lines, then split the lines into words so that
    > "hello there\nTommy Boy"
    > becomes
    > [ ["hello", "there"], ["Tommy", "Boy"] ]
    >
    > 2. Should I do this a different way?
    >
    > Thanks for any help!


    Your yo vector is empty. You need to insert into it before you can use it.

    std::vector mama;
    mama.push_back( "hello" );
    yo.push_back( mama );
    Jim Langston, Aug 13, 2006
    #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. Alex Hunsley
    Replies:
    17
    Views:
    849
  2. Pud
    Replies:
    0
    Views:
    563
  3. Replies:
    0
    Views:
    515
  4. Ivan Vecerina
    Replies:
    0
    Views:
    476
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,573
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page