References and multi-data lists

Discussion in 'C++' started by Zaharije Pasalic, Aug 23, 2004.

  1. I tried to implement multi-data list with a reference instead of
    pointer to data, but code compiled with gcc perform "segmentation
    fault". Generated code from other compilers: Borland C++ 5.0 and
    Micros. C++ 6.0 is OK. What is wrong?


    #include <iostream>
    using namespace::std;

    class base {
    public:
    virtual void dump() = 0;
    };

    class chr : public base {
    char ch;
    public:
    chr(char s) { ch = s; }
    ~chr() { }
    void dump() { cout << ch ; }
    };

    class num : public base {
    int n;
    public:
    num(int i) {n = i; }
    ~num() {}
    void dump() { cout << n; }
    };

    struct node {
    base &data;
    node *next;
    node(base &d) : data(d) {}
    };

    class list {
    node *n;
    public:
    list() : n(0) {}
    void add(base &d);
    void dump() {
    for(node *tmp = n; tmp; tmp = tmp->next)
    /* THIS IS LINE WHERE SEGMENTATION OCCURS */
    tmp->data.dump();
    }
    };

    void list::add(base &d)
    {
    node *tmp = n;
    if (!n) {
    n = new node(d);
     
    Zaharije Pasalic, Aug 23, 2004
    #1
    1. Advertising

  2. Zaharije Pasalic

    Rolf Magnus Guest

    Zaharije Pasalic wrote:

    > I tried to implement multi-data list with a reference instead of
    > pointer to data, but code compiled with gcc perform "segmentation
    > fault". Generated code from other compilers: Borland C++ 5.0 and
    > Micros. C++ 6.0 is OK. What is wrong?
    >
    >
    > #include <iostream>
    > using namespace::std;
    >
    > class base {
    > public:
    > virtual void dump() = 0;
    > };
    >
    > class chr : public base {
    > char ch;
    > public:
    > chr(char s) { ch = s; }
    > ~chr() { }
    > void dump() { cout << ch ; }
    > };
    >
    > class num : public base {
    > int n;
    > public:
    > num(int i) {n = i; }
    > ~num() {}
    > void dump() { cout << n; }
    > };
    >
    > struct node {
    > base &data;
    > node *next;
    > node(base &d) : data(d) {}
    > };
    >
    > class list {
    > node *n;
    > public:
    > list() : n(0) {}
    > void add(base &d);
    > void dump() {
    > for(node *tmp = n; tmp; tmp = tmp->next)
    > /* THIS IS LINE WHERE SEGMENTATION OCCURS */
    > tmp->data.dump();
    > }
    > };
    >
    > void list::add(base &d)
    > {
    > node *tmp = n;
    > if (!n) {
    > n = new node(d);


    You didn't show how you use your list. Are you sure none of the objects
    you reference went out of scope before you traverse your list?
     
    Rolf Magnus, Aug 23, 2004
    #2
    1. Advertising

  3. Zaharije Pasalic wrote:
    >
    > I tried to implement multi-data list with a reference instead of
    > pointer to data, but code compiled with gcc perform "segmentation
    > fault". Generated code from other compilers: Borland C++ 5.0 and
    > Micros. C++ 6.0 is OK. What is wrong?
    >
    > #include <iostream>
    > using namespace::std;
    >
    > class base {
    > public:
    > virtual void dump() = 0;
    > };
    >
    > class chr : public base {
    > char ch;
    > public:
    > chr(char s) { ch = s; }
    > ~chr() { }
    > void dump() { cout << ch ; }
    > };
    >
    > class num : public base {
    > int n;
    > public:
    > num(int i) {n = i; }
    > ~num() {}
    > void dump() { cout << n; }
    > };
    >
    > struct node {
    > base &data;
    > node *next;
    > node(base &d) : data(d) {}
    > };


    How about initializing next to something usefull ?

    >
    > class list {
    > node *n;
    > public:
    > list() : n(0) {}
    > void add(base &d);
    > void dump() {
    > for(node *tmp = n; tmp; tmp = tmp->next)
    > /* THIS IS LINE WHERE SEGMENTATION OCCURS */
    > tmp->data.dump();
    > }
    > };


    When working with pointers, always check if they point to
    something usefull. Part of that check is if they get
    initialized to something usefull.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 23, 2004
    #3
  4. Zaharije Pasalic

    Ron Natalie Guest

    "Zaharije Pasalic" <> wrote in message news:...
    > I tried to implement multi-data list with a reference instead of
    > pointer to data, but code compiled with gcc perform "segmentation
    > fault". Generated code from other compilers: Borland C++ 5.0 and
    > Micros. C++ 6.0 is OK. What is wrong?
    >


    Your example is incomplete...but one guess, are you sure the objects passed
    to list::add continue to exist when the dump routine is called?
     
    Ron Natalie, Aug 23, 2004
    #4
  5. Zaharije Pasalic

    Ron Natalie Guest

    "Karl Heinz Buchegger" <> wrote in message news:...
    >> When working with pointers, always check if they point to

    > something usefull. Part of that check is if they get
    > initialized to something usefull.
    >
    > --


    ....of course, it's not always possible to check to see if a pointer
    or reference is still valid. As I suspect in this case, the references
    refer to objects that have ceased to be, expired and gone to meet
    it's maker, bereft of life, run down the curtain and joined the choir
    invisible...
     
    Ron Natalie, Aug 23, 2004
    #5
  6. Ron Natalie wrote:
    >
    > "Karl Heinz Buchegger" <> wrote in message news:...
    > >> When working with pointers, always check if they point to

    > > something usefull. Part of that check is if they get
    > > initialized to something usefull.
    > >
    > > --

    >
    > ...of course, it's not always possible to check to see if a pointer
    > or reference is still valid.


    True.
    But surpisingly often, if I have a pointer, a quick look
    at the thing it is pointing to with the debugger
    shows that the pointer is garbage.
    Anyway, what I wanted to get at, was that the OP didn't show
    code to set the next pointer in his structure to NULL. So
    when he adds the first struct to his list, I guess he is
    expecting a 0 pointer in there, which is not there. And that
    *can* be detected easily with the debugger

    > As I suspect in this case, the references
    > refer to objects that have ceased to be, expired and gone to meet
    > it's maker, bereft of life, run down the curtain and joined the choir
    > invisible...


    Could also be. The OP simply presented not enough code to
    decide.
    In fact the OP needs to learn one lesson: When
    dealing with dynamic data structures (own implemented) and
    the display code crashes, then most likely it is not the
    display code that is in error, but the code that builds
    up the structure. At least that was my experience during
    my apprentice years :)

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 23, 2004
    #6
  7. Zaharije Pasalic

    Jon Bell Guest

    In article <4129ea97$0$2419$>,
    Ron Natalie <> wrote:
    >
    >...of course, it's not always possible to check to see if a pointer
    >or reference is still valid. As I suspect in this case, the references
    >refer to objects that have ceased to be, expired and gone to meet
    >it's maker, bereft of life, run down the curtain and joined the choir
    >invisible...


    In other words, they're ex-objects!

    Monty C++, anyone?

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Aug 23, 2004
    #7
  8. "Ron Natalie" <> wrote in message news:<4129e170$0$2465$>...
    > "Zaharije Pasalic" <> wrote in message news:...
    > > I tried to implement multi-data list with a reference instead of
    > > pointer to data, but code compiled with gcc perform "segmentation
    > > fault". Generated code from other compilers: Borland C++ 5.0 and
    > > Micros. C++ 6.0 is OK. What is wrong?
    > >

    >
    > Your example is incomplete...but one guess, are you sure the objects passed
    > to list::add continue to exist when the dump routine is called?


    OK! Code is not so good (maybe has errors) becuase I wrote this
    without compiling :( Main problem was adding new node when temporaly
    object is created, so after calling "add" object is gonne forevere, or
    better:

    "expired and gone to meet it's maker, bereft of life, run down the
    curtain and joined the choir invisible..." (Thanks Ron for this
    description :)

    Main reaseon for this is my thinking that temporaly object MUST exist
    if exist reference to him. So, i was wrong!

    Thanks a lot.
     
    Zaharije Pasalic, Sep 9, 2004
    #8
    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. Roger Leigh
    Replies:
    8
    Views:
    473
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    484
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    641
    DanielEKFA
    May 16, 2005
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    441
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Daniel Nogradi
    Replies:
    3
    Views:
    370
    Dennis Lee Bieber
    Nov 10, 2006
Loading...

Share This Page