My issue related to GLib, C programming. When I initialize the struct GHashtable.
我的问题与GLib,C编程有关。当我初始化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.
在我的hash_table中,我有三个数组来存储键,值和哈希值。
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.
我的问题是,当我在分配内存后显示hash_tables时,我发现在values数组中存储了一个数字。
[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?
我是否必须手动初始化键,值和散列数组,在为它们分配内存后将值分配为0或NULL?
Without assigning them as 0 or NULL, some random numbers or garbage numbers will come out like that?
如果不将它们指定为0或NULL,那么会出现一些随机数或垃圾数?
Thank you.
2 个解决方案
#1
0
From the man page for malloc()
:
从malloc()的手册页:
malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.
malloc()分配大小字节并返回指向已分配内存的指针。内存未清除。
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:
如果要将已分配的内存清零,则需要使用memset来执行此操作:
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:
对于您的数组,您可以使用calloc来执行此操作,这也具有将已分配的内存清零的副作用:
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.
另请注意,malloc函数族的返回值不应该在C中进行类型化。这样做可以隐藏代码中的错误,例如#include
#2
5
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.
你不应该自己初始化GHashTable。 GHashTable结构在公共标头中是不完整的,并且是私有API。它可以更改,恕不另行通您应该调用g_hash_table_new(或g_hash_table_new_full),它将分配内存并正确初始化哈希表。
#1
0
From the man page for malloc()
:
从malloc()的手册页:
malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.
malloc()分配大小字节并返回指向已分配内存的指针。内存未清除。
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:
如果要将已分配的内存清零,则需要使用memset来执行此操作:
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:
对于您的数组,您可以使用calloc来执行此操作,这也具有将已分配的内存清零的副作用:
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.
另请注意,malloc函数族的返回值不应该在C中进行类型化。这样做可以隐藏代码中的错误,例如#include
#2
5
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.
你不应该自己初始化GHashTable。 GHashTable结构在公共标头中是不完整的,并且是私有API。它可以更改,恕不另行通您应该调用g_hash_table_new(或g_hash_table_new_full),它将分配内存并正确初始化哈希表。