My issue related to GLib, C programming. When I initialize the struct GHashtable.
struct _GHashTable
{
gint size;
gint mod;
guint mask;
gint nnodes;
gint noccupied; /* nnodes + tombstones */
gpointer *keys;
guint *hashes;
gpointer *values;
GHashFunc hash_func;
GEqualFunc key_equal_func;
gint ref_count;
GDestroyNotify key_destroy_func;
GDestroyNotify value_destroy_func;
};
GHashTable *hash_table;
hash_table = (GHashTable *)malloc(sizeof(GHashTable));
Inside my hash_table, I have three arrays to store the keys, values, and hashes.
gpointer *keys;
guint *hashes;
gpointer *values;
I initialize those arrays in the initialization function:
hash_table->keys = malloc(sizeof(gpointer) * hash_table->size);
hash_table->values = hash_table->keys;
hash_table->hashes = malloc(sizeof(guint) * hash_table->size);
My question is when I display the hash_tables after I allocate memory, I found there is a number stored in the values array.
[0] key: (null) hash: 0 values: (null)
[1] key: (null) hash: 0 values: (null)
// Where is the 64273 comes from? Why other array are
// initialized as 0 or null. Only this index is initialized like that?
[2] key: (null) hash: 64273 values: (null)
[3] key: (null) hash: 0 values: (null)
[4] key: (null) hash: 0 values: (null)
[5] key: (null) hash: 0 values: (null)
[6] key: (null) hash: 0 values: (null)
[7] key: (null) hash: 0 values: (null)
Do I have to initialize the keys, values and hashes array manually, assigning the values as 0 or NULL after allocating the memory for them?
Without assigning them as 0 or NULL, some random numbers or garbage numbers will come out like that?
Thank you.