References and multi-data lists

Z

Zaharije Pasalic

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);
 
R

Rolf Magnus

Zaharije said:
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?
 
K

Karl Heinz Buchegger

Zaharije said:
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.
 
R

Ron Natalie

Zaharije Pasalic said:
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?
 
R

Ron Natalie

Karl Heinz Buchegger said:
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...
 
K

Karl Heinz Buchegger

Ron said:
...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 :)
 
J

Jon Bell

...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?
 
Z

Zaharije Pasalic

Ron Natalie said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top