473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

Aug 1 '05 #1
2 1952
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.