473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

References and multi-data lists

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
7 1852
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
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

"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

"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
2
by: Oliver Sturm | last post by:
Say I have a data class: class Data { public List<SubData> SubDataList1 { get ... set ... } public List<Subdata> SubDataList2 { get ... set ... } } It's possible that a specific SubData...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
4
by: Claire | last post by:
Visual studio .net 2003. We have created 2 solutions that are related to and dependent on each other's class libraries. One of them is executed as required, the other one sits in the task bar....
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.