2014-12-27 46 views
0

我有一個老調重彈功能哈希表所示:設置指針引用

int hash_rehash(hash_table *ht) { 
    list *l = hash_list(ht); // creates a linked list of hash table members 
    hash_table *nht = hash_createTable(ht->buckets * 2, ht->maxLoad, ht->hash); // new hash table with 2x the buckets 
    unsigned int i = 0; 
    list_node *c = l->head; 
    hash_destroyTable(ht); // destroy the old hash table 
    for (; i < l->size; i++, c = c->next) { 
     hash_insert(nht, c->data); // loop through linked list to re-insert old hash table members 
    } 
    *ht = *nht; // reference of old hash table = reference of new hash table? 
    list_destroyList(l); 
    return 0; 
} 

但是,如果我摧毀舊哈希表中我讀出了新表,因爲所有的成員時,碰上一個SIGABRT錯誤NT正在使用我爲ht分配的相同內存。什麼是正確的方法來改變舊的哈希表ht引用新的哈希表nht?

回答

1

接受hash_table **而不是hash_table *。然後,如果您的呼叫者有hash_table *ht,他們將撥打hash_rehash(&ht),允許該功能修改ht

+0

是否每個調用hash_rehash的函數也必須接受hash_table **? – user2506293

+0

@ user2506293取決於它如何使用它,但很可能是的。 – hobbs