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 

Puzzle

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
nrivera.eng@gmail.com
Guest





PostPosted: Mon Aug 01, 2005 6:05 pm    Post subject: Puzzle Reply with quote



Gurus,

This is the problem:

The code below outputs bogus data when the either: *( iter->second )
is used and causes a segfault or *(tools[iter->first]) is used the
curious things is that the output of iter->first is perfect and when
the input is verified by:

for(int i = 0 ; inventory && !inventory.eof(); i++ )
{
Tool tempTool;
inventory >> tempTool;

tools.insert( Record::value_type( tempTool.getRecordNum(),
&tempTool ) );

cout << *(tools[tempTool.getRecordNum()]); // Input verification
}

The data seems to be correctly input into the Map data structure so
WHY THE BOGUS OUTPUT and the SEGFAULT?

Here is the Output portion:

for( Record::const_iterator iter = tools.begin(); iter !=
tools.end(); ++iter)
{
cout << *(tools[iter->first])<< endl;
}

were Record is:

typedef map < int, Tool *, less< int > > Record;

and Tools is a ( int, string, int, double ) object

the << operator has been overloaded and verified.

Thanks,
Noel


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Aug 02, 2005 12:02 am    Post subject: Re: Puzzle Reply with quote



* [email]nrivera.eng (AT) gmail (DOT) com[/email]:
Quote:
asdølkjh

You've already got excellent answers in [comp.lang.c++], namely, don't store
pointers to local variables that go out of scope.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Sat Aug 06, 2005 11:20 am    Post subject: Re: Puzzle Reply with quote



[email]nrivera.eng (AT) gmail (DOT) com[/email] wrote:
Quote:
Gurus,

This is the problem:

The code below outputs bogus data when the either: *( iter->second )
is used and causes a segfault or *(tools[iter->first]) is used the
curious things is that the output of iter->first is perfect and when
the input is verified by:

for(int i = 0 ; inventory && !inventory.eof(); i++ )
{
Tool tempTool;

Try:

Tool *tempTool = new tempTool;

Quote:
inventory >> tempTool;

inventory >> *tempTool;
Quote:

tools.insert( Record::value_type( tempTool.getRecordNum(),
&tempTool ) );

tools.insert( Record::value_type( tempTool.getRecordNum(),
tempTool ) );


Quote:
cout << *(tools[tempTool.getRecordNum()]); // Input verification
}

In your original formulation, you're keeping the address of objects
that cease to exist when you hit the last line of the loop body.

The 'first' part of the value_type, being an integer, is not affected
by the transient nature of the object; however, the second part is
a "Tool *", and all of your Tool*'s point to objects that have ceased
to exist.

The modifications suggested above will create heap-based objects that
will survive the lexical scope of their creation.

You will need to delete these objects at some point, of course.

Quote:

The data seems to be correctly input into the Map data structure so
WHY THE BOGUS OUTPUT and the SEGFAULT?

Here is the Output portion:

for( Record::const_iterator iter = tools.begin(); iter !=
tools.end(); ++iter)
{
cout << *(tools[iter->first])<< endl;
}

were Record is:

typedef map < int, Tool *, less< int > > Record;

and Tools is a ( int, string, int, double ) object

the << operator has been overloaded and verified.
--

A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.