C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

References and multi-data lists

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Zaharije Pasalic
Guest





PostPosted: Mon Aug 23, 2004 11:57 am    Post subject: References and multi-data lists Reply with quote



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);
Back to top
Rolf Magnus
Guest





PostPosted: Mon Aug 23, 2004 12:20 pm    Post subject: Re: References and multi-data lists Reply with quote



Zaharije Pasalic wrote:

Quote:
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?


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Aug 23, 2004 12:21 pm    Post subject: Re: References and multi-data lists Reply with quote



Zaharije Pasalic wrote:
Quote:

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 ?

Quote:

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Ron Natalie
Guest





PostPosted: Mon Aug 23, 2004 12:26 pm    Post subject: Re: References and multi-data lists Reply with quote


"Zaharije Pasalic" <zaharije.pasalic (AT) pmf (DOT) unsa.ba> wrote

Quote:
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?



Back to top
Ron Natalie
Guest





PostPosted: Mon Aug 23, 2004 1:05 pm    Post subject: Re: References and multi-data lists Reply with quote


"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote

Quote:
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...


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Aug 23, 2004 1:49 pm    Post subject: Re: References and multi-data lists Reply with quote

Ron Natalie wrote:
Quote:

"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote

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

Quote:
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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Jon Bell
Guest





PostPosted: Mon Aug 23, 2004 2:28 pm    Post subject: Re: References and multi-data lists Reply with quote

In article <4129ea97$0$2419$9a6e19ea (AT) news (DOT) newshosting.com>,
Ron Natalie <ron (AT) sensor (DOT) com> wrote:
Quote:

...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 <jtbellm4h (AT) presby (DOT) edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Back to top
Zaharije Pasalic
Guest





PostPosted: Thu Sep 09, 2004 12:55 pm    Post subject: Re: References and multi-data lists Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:
"Zaharije Pasalic" <zaharije.pasalic (AT) pmf (DOT) unsa.ba> 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?


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 Sad 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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.