g_hash_table_insert似乎覆盖了先前插入的值

时间:2018-08-23 13:24:15

标签: c hashtable glib

我是GLIB库的新手。 g_hash_table_insert()似乎覆盖了先前插入的值。当我同时使用键和已保存数据的值进行打印时,键仍然是唯一且不变的,但它们的值 ALL 是相同的。我正在将struct类型存储到GHashtable中,这是它的结构:

struct _DsectionEntity {
  ...
  int entity_type;
  int sequence_number;
  ...
};

typedef struct _DsectionEntity DsectionEntity;

我逐行解析IGES模型文件,并在解析IGES文件的D部分的2行之后创建DsectionEntity对象。我将对象的序列号用作键,将整个对象用作值。创建哈希表和插入值的代码如下:

void
get_dsection(IgesFile *fp, DsectionEntity *ds)
{
  char *line1 = malloc(91);
  char *line2 = malloc(91);

  /* dsection_ht GHashtable declared as a global var and initialized to NULL */
  dsection_ht = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);

  line1 = get_line(fp, line1);
  while (line1) {
    if (line1[72] == 'D') {
      line2 = get_line(fp, line2);

      /* create Object */
      parser_dsection_new(ds, line1, line2);

      /* insert Object into the hashtable */
      parser_add_ds_object(dsection_ht, ds);
      line1 = get_line(fp, line1);
    } else {
      line1 = get_line(fp, line1);
    }
  }
}

插入代码:

void
parser_add_ds_object(GHashTable * ht, DsectionEntity *dsec_entity)
{
  // printf("KEY : %d\n", GPOINTER_TO_INT(GINT_TO_POINTER(dsec_entity->sequence_number)));
  // printf("SQ : %d\n", dsec_entity->sequence_number);
  // printf("Entity : %d\n", dsec_entity->entity_type);
  // printf("\n");
  g_hash_table_insert(ht, GINT_TO_POINTER(dsec_entity->sequence_number), (gpointer)dsec_entity);
}

如果在printf上删除了注释,则输出(正确的)输出为:

enter image description here

void
print_values(gpointer key, gpointer value, gpointer userdata)
{
  int realkey = GPOINTER_TO_INT(key);
  DsectionEntity *realvalue = (DsectionEntity *)value;

  printf("KEY : %d\n", realkey);
  printf("SQ : %d\n", realvalue->sequence_number);
  printf("Entity : %d\n", realvalue->entity_type);
  printf("====================================\n");
}

如果我使用上面显示的g_hash_table_foreach(dsection_ht, print_values, NULL)print_values()显示。我得到:

enter image description here

具有重复的sequence_numberentity_type的对象是添加到GHashtable中的最后一个对象(如图1所示)。 Valgrind没有显示错误,那么可能是什么问题?

文件格式为IGES(原始图形交换规范)。 parser_dsection_new()的代码:

void
parser_dsection_new(DsectionEntity *dsec_entity, char *line1, char *line2)
{
  char substr[10];

  get_field(line1, substr, 1, 8);
  dsec_entity->entity_type = utils_to_int(substr);

  // ...

  get_field(line1, substr, 74, 8);
  dsec_entity->sequence_number = utils_to_int(substr);

  get_field(line1, substr, 9, 8);
  dsec_entity->line_weight = utils_to_int(substr);

  get_field(line1, substr, 17, 8);
  dsec_entity->color = utils_to_int(substr);

  get_field(line1, substr, 57, 8);
  dsec_entity->entity_label = substr;
  // ...
}

1 个答案:

答案 0 :(得分:2)

根据g_hash_table_insert()的引用:

  

将新的键和值插入GHashTable。

     

如果密钥已存在于GHashTable中,则其当前值将替换为新值。如果在创建GHashTable时提供了value_destroy_func,则使用该函数释放旧值。如果在创建GHashTable时提供了key_destroy_func,则使用该函数释放传递的密钥。

因此,g_hash_table_insert() API应该用新值替换现有值。


但是,您的问题是您仅使用DsectionEntity的一个实例进行解析,插入等操作,而每次插入新的键/值对时都应该有一个唯一的实例在哈希中。在您的代码中,同一实例被覆盖,这就是为什么您仅看到最新值的原因。使用唯一的实例,它将起作用。

您可以在g_hash_table_new_full()g_hash_table_remove_all()中使用默认删除功能,如下所示:

// Create hash table with default delete callbacks
ht = g_hash_table_new_full(g_direct_hash, g_direct_equal, g_free, g_free);
//                                                        ~~~~~~  ~~~~~~

// Allocate new entry
ds = (DsectionEntity*) malloc( sizeof(DsectionEntity) );

// Populate entry
// ...

// Insert entry in hash table
g_hash_table_insert( ht, 
                     GINT_TO_POINTER( ds->sequence_number ), 
                     (gpointer) ds);

// ^^^ Do check the return value of g_hash_table_insert

// Remove hash table at the end
g_hash_table_remove_all( ht );

选中此example以获取指导。


替代:

您可能想探索UT Hash作为替代哈希表解决方案。