Puzzle

N

nrivera.eng

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
 
A

Alf P. Steinbach

* (e-mail address 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.
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top