Segmentation fault...

Discussion in 'C++' started by deancoo, Feb 14, 2005.

  1. deancoo

    deancoo Guest

    Ok, I've got another one for ya. The code I've pasted below compiles fine
    but produces a seg fault. I've put a comment in where the seg fault gets
    tripped. Can someone please explain what I've done wrong. Thanks again for
    any help.

    d

    #include <cstdlib>
    #include <iostream>
    #include <vector>

    using namespace std;

    class hand {
    public:
    vector<int> my_ints;
    };

    class hand_vector {
    public:
    vector<hand> myhands;
    };

    int main(int argc, char *argv[])
    {

    system("PAUSE");

    hand_vector *myvector;
    hand v_stub;

    for (int i=1; i<=10; i++) {
    myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE
    };

    system("PAUSE");

    vector<hand>::iterator j;

    for (j=myvector->myhands.begin(); j!=myvector->myhands.end(); j++) {
    j->my_ints.push_back(0);
    j->my_ints.push_back(0);
    j->my_ints.push_back(0);
    j->my_ints.push_back(0);
    j->my_ints.push_back(0);
    };

    system("PAUSE");

    delete myvector;

    system("PAUSE");
    return EXIT_SUCCESS;
    }
     
    deancoo, Feb 14, 2005
    #1
    1. Advertising

  2. "deancoo" <> wrote in message
    news:Ns0Qd.46789$gA4.28064@edtnps89...
    > Ok, I've got another one for ya. The code I've pasted below compiles fine
    > but produces a seg fault. I've put a comment in where the seg fault gets
    > tripped. Can someone please explain what I've done wrong. Thanks again
    > for any help.

    ....
    > int main(int argc, char *argv[])
    > {
    >
    > system("PAUSE");
    >
    > hand_vector *myvector;

    ....
    > for (int i=1; i<=10; i++) {
    > myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE
    > };

    ....
    > delete myvector;


    Don't use a pointer when you need a local variable !

    Define myvector as:
    hand_vector myvector;
    and use it with '.':
    myvector.myhands.push_back(...)
    It will automatically be disposed of upon function exit.


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, Feb 14, 2005
    #2
    1. Advertising

  3. deancoo wrote:
    >
    > int main(int argc, char *argv[])
    > {
    >
    > system("PAUSE");
    >
    > hand_vector *myvector;


    myvector is a pointer. It points to ... nowhere in particular

    > hand v_stub;
    >
    > for (int i=1; i<=10; i++) {
    > myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE


    So where is the hand_vector object, where myvector is supposed to point to?
    It was never allocated. All you have is a pointer that can point to such
    an object. But having a pointer and having the object itself are different
    things. It is as having a description of a Ferrari compared to having a
    Ferrari itself.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 14, 2005
    #3
  4. deancoo

    deancoo Guest

    Doh! Thanks Karl, that's what happens when you work too late. Further,
    after correcting this problem by creating an instance on the heap with
    "new", I believe there is a memory leak of some sort. After deleting the
    "hand_vector" instance, not all memory is released (only about 30%). It
    appears to me as though the instances of "hand" are still hanging around.
    Are they not on the heap? Shouldn't they be released along with the
    "hand_vector" instance?

    d

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > deancoo wrote:
    >>
    >> int main(int argc, char *argv[])
    >> {
    >>
    >> system("PAUSE");
    >>
    >> hand_vector *myvector;

    >
    > myvector is a pointer. It points to ... nowhere in particular
    >
    >> hand v_stub;
    >>
    >> for (int i=1; i<=10; i++) {
    >> myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE

    >
    > So where is the hand_vector object, where myvector is supposed to point
    > to?
    > It was never allocated. All you have is a pointer that can point to such
    > an object. But having a pointer and having the object itself are different
    > things. It is as having a description of a Ferrari compared to having a
    > Ferrari itself.
    >
    > --
    > Karl Heinz Buchegger
    >
     
    deancoo, Feb 14, 2005
    #4
  5. What don't you use a debugger and put a breakpoint in the destructor
    of Hand and count the number of calls to the destructor to convince
    yourself you don't a leak, at least not ther.e



    "deancoo" <> wrote in message
    news:_z8Qd.36$%y.22@clgrps12...
    > Doh! Thanks Karl, that's what happens when you work too late. Further,
    > after correcting this problem by creating an instance on the heap with
    > "new", I believe there is a memory leak of some sort. After deleting the
    > "hand_vector" instance, not all memory is released (only about 30%). It
    > appears to me as though the instances of "hand" are still hanging around.
    > Are they not on the heap? Shouldn't they be released along with the
    > "hand_vector" instance?
    >
    > d
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > > deancoo wrote:
    > >>
    > >> int main(int argc, char *argv[])
    > >> {
    > >>
    > >> system("PAUSE");
    > >>
    > >> hand_vector *myvector;

    > >
    > > myvector is a pointer. It points to ... nowhere in particular
    > >
    > >> hand v_stub;
    > >>
    > >> for (int i=1; i<=10; i++) {
    > >> myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE

    > >
    > > So where is the hand_vector object, where myvector is supposed to point
    > > to?
    > > It was never allocated. All you have is a pointer that can point to such
    > > an object. But having a pointer and having the object itself are

    different
    > > things. It is as having a description of a Ferrari compared to having a
    > > Ferrari itself.
    > >
    > > --
    > > Karl Heinz Buchegger
    > >

    >
    >
     
    Dave Townsend, Feb 16, 2005
    #5
    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:
    871
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    532
  4. Ivan Vecerina
    Replies:
    0
    Views:
    488
    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,624
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page