 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
nrivera.eng@gmail.com Guest
|
Posted: Mon Aug 01, 2005 6:05 pm Post subject: Puzzle |
|
|
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
|
Posted: Tue Aug 02, 2005 12:02 am Post subject: Re: Puzzle |
|
|
* [email]nrivera.eng (AT) gmail (DOT) com[/email]:
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
|
Posted: Sat Aug 06, 2005 11:20 am Post subject: Re: Puzzle |
|
|
[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 |
|
 |
|
|
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
|
|