C 프로그램에서 GLib의 해시 테이블 구현을 사용하려고합니다. 단지 지금은 으로 실험하고 있습니다.GLib 해시 테이블 루프 문제
#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;
}
어떻게 든 내 테스트 코드를 반복 올바르게하지만 루프에서 항상 마지막 키와 마지막 값 쌍을 반환하고 항상 동일합니다 : 나는 테스트를 위해 다음 코드 조각을 썼다. 여기에 어떤 문제가 있습니까? 위의 코드는 형식 그대로 실행되지 않을 수 있습니다. 나는 지금 내가하려고하는 것에 대해 명확한 생각을주기 위해 일부 부분을 복사하여 붙여 넣었다.
고맙습니다. – systemsfault
sizeof (C99 6.5.3)의 문법은'sizeof unary-expression | sizeof (type-name)'이며,이 예제에서는 유형 이름 만 사용되므로 여기에 괄호가 필요합니다. –
나는 glib을 오랫동안 사용해 왔으며, 이런 것들을 찾아내는 데 도움이됩니다. :) – unwind