0

我想使用 hsearch_r 添加指向哈希表的指针。目前它不能使用以下代码段(没有变量声明和检查):

// Allocate hash table
htab = calloc( INITIAL_HASH_SIZE, sizeof(struct hsearch_data) );
hcreate_r( INITIAL_HASH_SIZE, htab );

// Add first pointer to hash table
he.key = (char *)&pointer_some_complex_struct1;
if ( hsearch_r( he, FIND, &hep, htab ) == 0) {
  he.data = pointer_some_complex_struct1->data;
  hsearch_r( he, ENTER, &hep, htab );
}

// Add second pointer to hash table
he.key = (char *)&pointer_some_complex_struct2;
if ( hsearch_r( he, FIND, &hep, htab ) ) {
  // CODE ENTERS HERE
}

无论对象不存在,第二个调用都会找到该对象。任何想法可能是上述代码段的问题?

4

1 回答 1

4

问题是 hsearch/hsearch_r 中的键是以 NUL 结尾的字符串,而不是任意数据。

于 2012-11-19T15:04:36.923 回答