[Bug libstdc++/13783] New: STL: hash_map bug: iterating thru the hash_map runs into an endless loop

siva at eternal-systems dot com gcc-bugzilla@gcc.gnu.org
Wed Jan 21 03:10:00 GMT 2004


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



More information about the Gcc-bugs mailing list