glib: hash table problems



Hi,

I've been trying to use the glib hash table, but can't get it to work.
Attached is the program which generates the following output
(under glib-1.1.10 CVS):

(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
10
11
12
13
14
(null)
16
17
18
19

I can't quite figure out why it doesn't just print 0-19 like it should. Can
 anybodypoint out to me what I'm doing wrong? It seems that the
hash table does not copy key values, so giving each item a new
string seems only appropriate butI'm still missing something.

It's probably something simple, but I can't seem to see it (maybe I've just
been looking at the same piece of code for too long) ...

Thanks
--> Robert

----------------------------------- cut here ------------------------------
------------
#include "glib.h"
#include <stdio.h>

guint TFWhash (gconstpointer key);
gint TFWhashcmp (gconstpointer a, gconstpointer b);

int main (int argc, char **argv)
{
     int       i;
     char      *s;
     GHashTable     *h;

     h = g_hash_table_new (TFWhash, TFWhashcmp);
     for (i=0; i<20; i++)
          {
          s = new char[80];

          sprintf (s, "%d", i);
          g_hash_table_insert (h, s, s);
          }


     s = new char[80];
     for (i=0; i<20; i++)
          {
          char      *v;

          sprintf (s, "%d", i);
          v = (char *) g_hash_table_lookup (h, s);
          printf ("%s\n", v);
          }
}


guint TFWhash (gconstpointer key)
{
        char    *s = (char *)key;
        int     i, v=0,
                l=strlen(s);

        for (i=0; i<l; i++)
                v = (64*v + s[i])%101;

        return (v);
}


gint TFWhashcmp (gconstpointer a, gconstpointer b)
{
        return (strcmp((char *)a,(char *)b));
}







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