Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Puzzle

Reply
Thread Tools

Puzzle

 
 
nrivera.eng@gmail.com
Guest
Posts: n/a
 
      08-01-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-01-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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 ) );


Address of local variable.

--
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-01-2005
(E-Mail Removed) wrote:
> 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 ) );


So, your 'tools' is defined to contain _addresses_, right? You're storing
the address of a local variable on every loop. Don't. Make your 'tools'
map contain the _objects_ instead.

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


Because as soon as the body of this loop finishes, the address you store
in the map becomes _invalid_.

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


Make it

typedef map<int,Tool> Record;

and when inserting, do

tools[tempTool.getRecordNum()] = tempTool;

or

tools.insert(make_pair(tempTool.getRecordNum(), tempTool));

>
> and Tools is a ( int, string, int, double ) object
>
> the << operator has been overloaded and verified.


V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the extra bar along top in v1.5 with puzzle image? Can we get rid of? S.Rodgers Firefox 9 12-14-2005 11:15 AM
Home networking puzzle?!? =?Utf-8?B?Qm9ya28=?= Wireless Networking 3 01-25-2005 06:49 AM
A NS puzzle:) Brad Snow Firefox 6 09-03-2004 04:54 AM
A puzzle to puzzle you sk A+ Certification 1 07-17-2004 05:19 PM
PUZZLE Getting DropDownList Index of Matching Value Earl Teigrob ASP .Net 3 08-06-2003 09:41 PM



Advertisments