Puzzle

Discussion in 'C++' started by nrivera.eng@gmail.com, Aug 1, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. * :
    > 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?
     
    Alf P. Steinbach, Aug 1, 2005
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Aug 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    3
    Views:
    6,652
    Nedu N
    Aug 6, 2003
  2. dwa

    Design Puzzle!

    dwa, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    370
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jun 10, 2004
  3. Shankara Narayanan

    Booking puzzle....

    Shankara Narayanan, Jun 17, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    933
    bredal Jensen
    Jun 30, 2004
  4. VB Programmer
    Replies:
    2
    Views:
    431
    Alan Lambert
    Nov 4, 2004
  5. G. Stewart

    regex puzzle!

    G. Stewart, Nov 23, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    518
    G. Stewart
    Nov 25, 2004
Loading...

Share This Page