Glib hash table - unkown key in table

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

 



Hello Glib users,

(I'm new in Glib, and I'm not a C expert :))

I would try to use Glib hash table to store many keys (strings) with its value (typedef unsigned char t_utype_rec_udatas[10];).

The problem is if I put a new key to table, and try to find it later (from another part of a complex code), the key doesn't exist.

When I put the key to table, I send a message to syslog with some info's, here is the code and its output:


    // fill hash with data's
    datastore = g_hash_table_new(g_str_hash, g_str_equal);
    while (fgets(inputbuffer, 90, fp) != NULL) {
        parse_utypeline(inputbuffer, udatakey, &udataidx);  // parse line, store fields in udatakey and udataidx
        utype_inc(udatakey, udataidx);  // push to hash
        total++;
    }

Mar 23 18:13:09 basil myprog: udata store: 0x2371400
Mar 23 18:13:09 basil myprog: new udatakey: 'ABCD', udataidx: 0
Mar 23 18:13:09 basil myprog: store value: 1
Mar 23 18:13:09 basil myprog: 0. key: 'ABCD'
Mar 23 18:13:09 basil myprog: size of hash: 1

So, there is a hast table on 0x2371400, and it contains 1 key: ABCD. This info has read by g_hash_table_get_keys(datastore) and g_hash_table_size(datastore). That's good.

Later, at some part of the code, I try to access this data - but then I get this info in syslog:

Mar 23 18:13:13 basil myprog: udata_get - udatakey: '', udataidx: 0
Mar 23 18:13:13 basil myprog: udata_get - size of hash: 1
Mar 23 18:13:13 basil myprog: udata_get - 0. key: '#020#001'
Mar 23 18:13:13 basil myprog: udata_get - udatakey doesn't exists in store: 0x2371400

So, the address of store is same as above, it still contains 1 key, but the key is '#020#001'. (I send this way:

gi = 0;
hkeys = g_hash_table_get_keys(datastore);
syslog(LOG_DEBUG, "udata_get - %d. key: '%s'", gi, hkeys->data);

Here is the code, what I use:

===%===
int utype_inc(char udatakey[15], int udata) {

    t_utype_rec_udatas *utype_rec_udatas;
    int i;
    gboolean gb;
    GList * hkeys = NULL;
    int gi;

    syslog(LOG_DEBUG, "utype store: %p", datastore);
    gb = g_hash_table_contains(datastore, udatakey);

    if (gb == 0) {
        utype_rec_udatas = g_malloc0(sizeof *utype_rec_udatas);
        for(i=0; i<10; i++) {
            /*
             * every item has derived like this:
             * typedef unsigned char t_utype_rec_udatas[10];
             * t_utype_rec_udatas *utype_rec_udatas;
             */
            (*utype_rec_udatas)[i] = 0;
        }
        g_hash_table_insert(datastore, udatakey, utype_rec_udatas);
    }
    else {
        utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
    }
    (*utype_rec_udatas)[udata]++;

    gb = g_hash_table_contains(datastore, udatakey);
    if (gb == 0) {
        return -1;
    }
    else {
        utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
        return (*utype_rec_udatas)[udata];
    }

}

int utype_get(char udatakey[15], int udata) {
    t_utype_rec_udatas *utype_rec_udatas;
    gboolean gb;

    if (datastore == NULL) {
        syslog(LOG_DEBUG, "store doesn't exists");
        return -1;
    }

    gb = g_hash_table_contains(datastore, udatakey);
    if (gb == 0) {
        return -1;
    }
    else {
        utype_rec_udatas = g_hash_table_lookup(datastore, udatakey);
        return (*utype_rec_udatas)[udata];
    }
}
===%===

I'm totally confised, could anybody helps me, what em'I wrong?


Thanks:

a.
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux