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

g_hash_table_insert seems to be overriding previously inserted values

我是 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 对象。我使用对象的序列号作为键,将整个对象作为值。创建散列 table 和插入值的代码如下:

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 上的评论,输出(正确的)是:

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()显示。我得到:

重复 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;
  // ...
}

根据g_hash_table_insert()的参考:

Inserts a new key and value into a GHashTable.

If the key already exists in the GHashTable its current value is replaced with the new value. If you supplied a value_destroy_func when creating the GHashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating the GHashTable, the passed key is freed using that function.

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


但是,你的问题是你只使用一个 DsectionEntity 的实例来处理所有事情,即解析、插入等,而每次你想插入一个新的 key/value 散列中的对。在您的代码中,同一个实例被覆盖,这就是您只看到最新值的原因。使用独特的实例,它将起作用。

您可以像这样在 g_hash_table_new_full() along with 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 作为替代哈希 table 解决方案。