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

References and multi-data lists

P: n/a
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);
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
kb******@gascad.at
Jul 22 '05 #2

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

Jul 22 '05 #3

P: n/a

"Zaharije Pasalic" <za**************@pmf.unsa.ba> wrote in message news:f8**************************@posting.google.c om...
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?
Jul 22 '05 #4

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
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...

Jul 22 '05 #5

P: n/a
Ron Natalie wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:41***************@gascad.at...
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
kb******@gascad.at
Jul 22 '05 #6

P: n/a
In article <41**********************@news.newshosting.com>,
Ron Natalie <ro*@sensor.com> 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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #7

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message news:<41**********************@news.newshosting.co m>...
"Zaharije Pasalic" <za**************@pmf.unsa.ba> wrote in message news:f8**************************@posting.google.c om...
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.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.