public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "siva at eternal-systems dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/13783] New: STL: hash_map bug: iterating thru the hash_map runs into an endless loop
Date: Wed, 21 Jan 2004 03:10:00 -0000	[thread overview]
Message-ID: <20040121031049.13783.siva@eternal-systems.com> (raw)

Hi,

Reading specs from /usr/lib/gcc-lib/i486-suse-linux/2.95.3/specs
gcc version 2.95.3 20010315 (SuSE)

The bug is reproducible even with the 3.2 version of g++ compiler.

Consider the following code:

# include <hash_map>
using namespace std;

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

typedef hash_map<const char*, int, hash<const char*>, eqstr> months_t;
int main()
{
  char c[256];
  months_t months;

  strcpy(c,"Jan");
  months[c]=10;

  strcpy(c,"Feb");
  months[c] = 20;

  months_t::iterator it = months.find("Jan");
  if(it==months.end())
  {
    cout << "No jan in list" << endl;
    cout << "Size = " << months.size() << endl;
  }
  else
      cout << "Iterator = " << it->first << endl;

  /* Uncomment this loop
  cout << "Printing months map" << endl;
  for(months_t::iterator i = months.begin(); i != months.end(); i++)
    cout << i->first << "\t" << i->second << endl;
  cout << "Completed Printing months map" << endl;
  */

  return(0);
}
********* end code **************

The code shown above runs into an endless loop.
I am not sure if that is the correct output.

I am changing the key value while retaining the same pointer (namely 'c').
I think the STL hash_map will take "Jan", hash it and store the pointer 'c' and 
the value 10.
Similarly, it will hash "Feb", hash it and store pointer 'c' and the value 20.

As a sideeffect of this, I would expect that if I did a months.find("Jan") and 
printed iterator->first, it should print "Feb" (bcos I copied "Feb" into 'c' at 
a later point in the code.). We hash "Jan" -> find the record { c, 10 } -> so 
if I printed it->first ('c'), it should print "Feb" and it->second should 
print '10'.
Instead the output says that it could not find "Jan", but the size of the 
hash_map is 2.

Also, the code runs into an infinite loop, when I uncomment the iterator loop.

Siva

-- 
           Summary: STL: hash_map bug: iterating thru the hash_map runs into
                    an endless loop
           Product: gcc
           Version: 2.95.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: siva at eternal-systems dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13783


             reply	other threads:[~2004-01-21  3:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-21  3:10 siva at eternal-systems dot com [this message]
2004-01-21  3:19 ` [Bug libstdc++/13783] " pinskia at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040121031049.13783.siva@eternal-systems.com \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).