I tried to implement multi-data list with a reference instead of
pointer to data, but code compiled with gcc perform "segmentati on
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); 7 1880
Zaharije Pasalic wrote: I tried to implement multi-data list with a reference instead of pointer to data, but code compiled with gcc perform "segmentati on 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
Zaharije Pasalic wrote: I tried to implement multi-data list with a reference instead of pointer to data, but code compiled with gcc perform "segmentati on 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?
"Zaharije Pasalic" <za************ **@pmf.unsa.ba> wrote in message news:f8******** *************** ***@posting.goo gle.com... I tried to implement multi-data list with a reference instead of pointer to data, but code compiled with gcc perform "segmentati on fault". Generated code from other compilers: Borland C++ 5.0 and Micros. C++ 6.0 is OK. What is wrong?
Your example is incomplete...bu t one guess, are you sure the objects passed
to list::add continue to exist when the dump routine is called?
"Karl Heinz Buchegger" <kb******@gasca d.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...
Ron Natalie wrote: "Karl Heinz Buchegger" <kb******@gasca d.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
In article <41************ **********@news .newshosting.co m>,
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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
"Ron Natalie" <ro*@sensor.com > wrote in message news:<41******* *************** @news.newshosti ng.com>... "Zaharije Pasalic" <za************ **@pmf.unsa.ba> wrote in message news:f8******** *************** ***@posting.goo gle.com... I tried to implement multi-data list with a reference instead of pointer to data, but code compiled with gcc perform "segmentati on fault". Generated code from other compilers: Borland C++ 5.0 and Micros. C++ 6.0 is OK. What is wrong?
Your example is incomplete...bu t 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
http://groups.google.com/groups?selm=bcq6fn%24g53%241%248300dec7%40news.demon.co.uk
This message summarizes some testing I've done and their results.
These results somewhat contradict Cornford's conclusions; I haven't
|
by: * ProteanThread * |
last post by:
but depends upon the clique:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den
...
|
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 and white CCITT4
compressed files (frames) inside the tiff. Every now and then I receive a
mixture of black and white CCITT4 and JPEG compressed files, and sometimes
just multi-page tiffs with JPEG only. The code runs great when dealing with
the...
|
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)?
|
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 instance is in SubDataList1 as
well as SubDataList2. When I serialize a Data instance, said SubData
instance is written to the XML file twice. After deserialization I have
| |
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 problem is the charset? How I can avoid this warning? But the worst
thing isn't the warning, but that the program doesn't work! The program
execute all other operations well, but it don't print the converted
letters: for example, in the string...
|
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. I'm working on the task bar application and my
colleagues are developing the other.
All of us need to do work on a single class library/project that is included
in both solutions.
When the solutions are finished they're both located in the same...
|
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 TIFF to several PNG files this causes a problem, becuase the
resulting image is (the page to the far left and a lot of black space
surrounding it and a filesize that is larger than needed.
Any ideas?
|
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 Systems
(MuCoCoS'08)
Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08.
<http://www.par.univie.ac.at/~pllana/mucocos08>
***********************************************************************
Context
|
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 "PAR.UniqueID" could not be bound.
The multi-part identifier "PAR.PAR_Status" could not be bound.
The multi-part identifier "Salary.New_Salary" could not be bound.
The multi-part identifier "Salary.UniqueID" could not be bound.
The multi-part...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |