简体   繁体   中英

Initialize the struct GHashTable in C

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.

You should not be initializing a GHashTable yourself. The GHashTable structure is incomplete in the public headers and is private API. It can change without notice. You should be calling g_hash_table_new (or g_hash_table_new_full ), which will allocate memory and properly initialize the hash table.

From the man page for malloc() :

malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

So memory allocated for your structure as well as your arrays are uninitialized. They could be zero or any other value.

If you want allocated memory to be zero'ed out, you need to use memset to do so:

hash_table = malloc(sizeof(GHashTable));
memset(hash_table, 0, sizeof(*hash_table));

In the case of your arrays, you can use calloc to do so, which also has the side effect of zeroing out the allocated memory:

hash_table->keys = calloc(hash_table->size, sizeof(gpointer));
hash_table->values = hash_table->keys;
hash_table->hashes = calloc(hash_table->size, sizeof(guint));

Also note that the return value of the malloc family of functions should NOT be type-casted in C. Doing so can hide errors in your code, such as a failure to #include <stdlib.h> Take a look at this question for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM