Inserting a non-pod structure into a GHashTable
I am trying to create a GHashTable from instances of a struct containing ints, time_t, and multiple char *.
My question is, how do you inject a struct instance into a GHashTable? there are many examples of how to insert a string or int (using g_str_hash and g_int_hash respectively), but my guess is that I want to use g_direct_hash and I can't find any examples of this.
Ideally, my code would look like this:
GHashtable table;
table = g_hash_table_new(g_direct_hash, g_direct_equal);
struct mystruct;
mystruct.a = 1;
mystruct.b = "hello";
mystruct.c = 5;
mystruct.d = "test";
g_hash_table_insert(table,mystruct.a,mystruct);
This is clearly not true since it does not compile. Can anyone provide an example that does what I want? Thanks, Rick
a source to share
You cannot insert an automatic variable; you need to allocate memory for storing data in a dynamic way, i.e. using g_malloc()
or equivalent.
Then you need to figure out a way to calculate the hash value from your data to help the table be efficient. The usage g_direct_hash()
is not very good here; it will use a pointer to your data as a hash value.
It seems that you want to use a member of a
your structure as a key; what type is this field? If it is an integer, you can use g_int_hash()
.
I think this is more like what your actual code looks like:
GHashtable *table;
struct mystruct *my;
table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, g_free);
my = g_malloc(sizeof *my);
my->a = 1;
my->b = "hello";
my->c = 5;
my->d = "test";
g_hash_table_insert(table, GINT_TO_POINTER(my->a), my);
Note that this assumes that b
and d
are simply pointers to characters, since no dynamic allocation is allocated for strings.
a source to share
You have to allocate structures on the heap to store the pointer in the hash table:
struct SomeType * p = malloc(sizeof(struct SomeType));
p->a = 1;
//etc..
g_hash_table_insert(table,p->a,p);
You will also need to use g_hash_table_new_full () so that you can deallocate the pointers correctly when the table is destroyed.
a source to share
Thanks. The above examples helped. By reading these and looking at code samples on the net, I could get it to work. Below is an example of a working code I wrote:
#include <stdio.h>
#include <glib.h>
#include <stdlib.h>
struct struct_process {
int pid;
char* file_to_process;
};
typedef struct struct_process Process;
int main() {
GHashTable* hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
Process* p1 = (Process*)(malloc(sizeof(Process)));
p1->pid = 1234;
p1->file_to_process= "/var/tmp/p1";
g_hash_table_insert(hash_table, GINT_TO_POINTER(p1->pid), GINT_TO_POINTER(p1));
# replace 1234 by some other key to see that it returns NULL on nonexistent keys
Process* p3 = (Process*)(g_hash_table_lookup(hash_table, GINT_TO_POINTER(1234)));
if (p3 == NULL) {
printf("could not find\n");
} else {
printf("found and i have to process %s\n", p3->file_to_process);
}
g_hash_table_destroy(hash_table);
}
`
a source to share