By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,111 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,111 IT Pros & Developers. It's quick & easy.

Puzzle

P: n/a
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

Aug 1 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* nr*********@gmail.com:
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?
Aug 1 '05 #2

P: n/a
nr*********@gmail.com 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
Aug 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.