Re: [gtk-list] (test case) GHash bug?

Jeff Garzik wrote:
> The following test (attached) inserts 20 values into a hash table.  The
> key is a number (0-19) stored as a string, hashed w/ a custom CRC hash
> function.  The value is a g_strdup'd copy of that same number, stored as
> a string formatted from "%d value".
> The current ghash code seems to be failing miserably on this easy test.
>         Jeff

>      g_print("create table\n");
>      h = g_hash_table_new (second_hash, second_hash_cmp);
>      for (i=0; i<20; i++)
>           {
>           sprintf (new_key, "%d", i);
>           g_assert (atoi (new_key) == i);
>           sprintf (new_val, "%d value", i);
>           g_assert (atoi (new_val) == i);
>           new_val_copy = g_strdup (new_val);
>           g_assert (new_val_copy != NULL);
>           g_assert (*new_val_copy != 0);

>           g_print("table insert %d, key=\"%s\", val=\"%s\"\n",
> 	  	i, new_key, new_val_copy);
>           g_hash_table_insert (h, new_key, new_val_copy);
>           }

You didn't copy the key. The hash table doesn't copy the key or the
value, so all the keys ended up pointing to the same buffer.

I tried it with code to copy the key and it was OK.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]