Pregunta

I'm working on a program, which uses Glib's hash table. When the program starts, it reads a file, and fill the hash table based those datas. When the program runs, the user gives their datas, and code checks the key exists - if yes, update the value, if not, insert a new key.

Here is the sample:

    // 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++;
    }


    // another part of code, find value of key
    nrofutype = utype_get(utypereclist.udatakey, udatainx);

// implemented functions
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];
    }
}

The problem is if the code calls utype_get() in later parts, the key didn't exists (which exists, that sure). If I put the syslog lines, and try to catch some data, I see the hash table address is same at every time. If the utype_inc() function log the udatakey, it seems in syslog correctly (I've logged the data from g_hash_table_get_keys(datastore);). But if utype_get() called, the g_hash_table_get_keys(datastore); gives some very strage info... from same store! Here is the syslog lines:

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

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

I don't know, what's the #020#001 (I know, that's possible a space, and a 0x01), but why?

Any helps are welcome,

thanks:

a.

¿Fue útil?

Solución

The solution was:

char datakeys[BIGNR][15];

// implemented functions
int utype_inc(char udatakey[15], int udata) {
...
    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;
        }
        gi = g_hash_table_size(datastore_store);
        strncpy(datakeys[gi], udatakey, strlen(udatakey));
        g_hash_table_insert(datastore, datakeys[gi], utype_rec_udatas);
    }
...
...

Thanks for the help,

a.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top