public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/13783] New: STL: hash_map bug: iterating thru the hash_map runs into an endless loop
@ 2004-01-21  3:10 siva at eternal-systems dot com
  2004-01-21  3:19 ` [Bug libstdc++/13783] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: siva at eternal-systems dot com @ 2004-01-21  3:10 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug libstdc++/13783] STL: hash_map bug: iterating thru the hash_map runs into an endless loop
  2004-01-21  3:10 [Bug libstdc++/13783] New: STL: hash_map bug: iterating thru the hash_map runs into an endless loop siva at eternal-systems dot com
@ 2004-01-21  3:19 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-21  3:19 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-21 03:19 -------
You cannot change the string from beneath hash_map.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-21  3:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-21  3:10 [Bug libstdc++/13783] New: STL: hash_map bug: iterating thru the hash_map runs into an endless loop siva at eternal-systems dot com
2004-01-21  3:19 ` [Bug libstdc++/13783] " pinskia at gcc dot gnu dot org

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).