C GLib哈希表循环问题

C GLib哈希表循环问题,c,hashtable,glib,C,Hashtable,Glib,我将在一个C程序中使用的Hash表实现 我只是在试验它。我编写了以下测试代码: #include <glib.h> #include <stdlib.h> #include <stdint.h> #include <stdio.h> #include <string.h> int main(){ // Some codes and declerations here GHashTable *g_hash_table;

我将在一个C程序中使用的Hash表实现 我只是在试验它。我编写了以下测试代码:

 #include <glib.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>

 int main(){
 // Some codes and declerations here
 GHashTable *g_hash_table;
 uint32_t *a;
 a=(uint32_t *)malloc(sizeof(uint32_t));
 if(a==NULL){
    printf("Not Enough Mem For a\n");
    return 1;
 }
 *a=1123231;

 uint32_t* key;
 key=(uint32_t *)malloc(sizeof(uint32_t));
 if(key==NULL){
     printf("Not Enough Mem For key\n");
     return 1;
 }
 *key=122312312;
 int i;
 g_hash_table=g_hash_table_new(g_int_hash, g_int_equal);
 for(i=0;i<TABLE_SIZE;i++){
     *key+=1;
     *a+=1;
     g_hash_table_insert(g_hash_table,(gpointer)key,(gpointer)a);
     uint32_t *x=(uint32_t *)g_hash_table_lookup(g_hash_table,key);
     printf("Counter:%d,  %u\n",i,*x);
 }

GHashTableIter iter;
g_hash_table_iter_init(&iter,g_hash_table);
int size=g_hash_table_size(g_hash_table);
printf("First size: %d\n",size);
uint32_t *val;
uint32_t *key_;
int counter=0;

// My problem is in the following loop it 
// always returns the same and the last key value pair
 while(g_hash_table_iter_next(&iter,(gpointer*)(void*)&key_,(gpointer*)(void*)&val)){
     counter++;
     printf("%u %u\n",(uint32_t)*key_,(uint32_t)*val);
     printf("Counter: %d\n",counter);
 }
 //Some more code here        
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){
//这里有一些代码和偏差
GHashTable*g_哈希表;
uint32_t*a;
a=(uint32_t*)malloc(sizeof(uint32_t));
如果(a==NULL){
printf(“没有足够的内存用于\n”);
返回1;
}
*a=1123231;
uint32_t*键;
key=(uint32_t*)malloc(sizeof(uint32_t));
if(key==NULL){
printf(“没有足够的内存用于密钥\n”);
返回1;
}
*密钥=122312312;
int i;
g_hash_table=g_hash_table_new(g_int_hash,g_int_equal);

对于(i=0;i我认为您的插入代码被破坏了。您只分配了一次内存,但随后进行了多次插入,增加了存储在每个位置之间的单个分配位置中的值

哈希表存储指针,因此它最终会将每个键与同一指针相关联

此外,为了保持一致性,您可能应该将
g_malloc()
与glib一起使用

我总是建议在对象上使用
sizeof
,而不是在它们的类型上;这样你就不会以一种非常危险的方式重复你自己了。因此

  guint32 *a;

  a = g_malloc(sizeof (guint32));
使用

通过这种方式,您可以“锁定”依赖项,以便始终分配足够的空间来存储
a
指向的任何内容,即使以后更改类型也是如此


此外,你应该仔细检查你所做的每一次转换。将任何非常量指针转换到
gpointer
是一个犹豫不决的程序员的标志。有了glib,
gpointer
仅仅是
void*
的同义词,因此根本不需要转换。它只会给代码增加粗糙度,使代码更难阅读。

中有一个错误<代码>键
a
声明。您总是在哈希表中放置相同的指针。请尝试:

#include <glib.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#define TABLE_SIZE 12

int main() {
    // Some codes and declarations here
    GHashTable *g_hash_table;
    int i;

    g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
    for (i=0; i<TABLE_SIZE; i++)
    {
        uint32_t* key = (uint32_t *)malloc(sizeof(uint32_t));
        uint32_t* a = (uint32_t *)malloc(sizeof(uint32_t));
        *key = i;
        *a   = i+10;
        g_hash_table_insert(g_hash_table, (gpointer)key, (gpointer)a);
        uint32_t *x = (uint32_t *)g_hash_table_lookup(g_hash_table,key);
        printf("key: %d -->  %u\n", *key ,*x);
    }

    GHashTableIter iter;
    int size=g_hash_table_size(g_hash_table);
    printf("First size: %d\n", size);

    uint32_t *val;
    uint32_t *key_;

    // My problem is in the following loop
    // it always returns the same and the last key value pair

    g_hash_table_iter_init (&iter, g_hash_table);
    while (g_hash_table_iter_next (&iter, (gpointer) &key_, (gpointer) &val))
    {
        printf("key %u ---> %u\n", (uint32_t)*key_, (uint32_t)*val);
    }

    // TODO: free keys
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义表格大小12
int main(){
//这里有一些代码和声明
GHashTable*g_哈希表;
int i;
g_hash_table=g_hash_table_new(g_int_hash,g_int_equal);

对于(i=0;Ithesizeof(C99 6.5.3)的语法是‘sizeof一元表达式| sizeof(type name)’,示例中唯一的用法是使用类型名,所以这里需要paren。我已经使用glib很长时间了,它有助于发现类似这样的东西。@Pete:是的,我将改写我的反对意见。
#include <glib.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#define TABLE_SIZE 12

int main() {
    // Some codes and declarations here
    GHashTable *g_hash_table;
    int i;

    g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
    for (i=0; i<TABLE_SIZE; i++)
    {
        uint32_t* key = (uint32_t *)malloc(sizeof(uint32_t));
        uint32_t* a = (uint32_t *)malloc(sizeof(uint32_t));
        *key = i;
        *a   = i+10;
        g_hash_table_insert(g_hash_table, (gpointer)key, (gpointer)a);
        uint32_t *x = (uint32_t *)g_hash_table_lookup(g_hash_table,key);
        printf("key: %d -->  %u\n", *key ,*x);
    }

    GHashTableIter iter;
    int size=g_hash_table_size(g_hash_table);
    printf("First size: %d\n", size);

    uint32_t *val;
    uint32_t *key_;

    // My problem is in the following loop
    // it always returns the same and the last key value pair

    g_hash_table_iter_init (&iter, g_hash_table);
    while (g_hash_table_iter_next (&iter, (gpointer) &key_, (gpointer) &val))
    {
        printf("key %u ---> %u\n", (uint32_t)*key_, (uint32_t)*val);
    }

    // TODO: free keys
    return 0;
}