我试图在节点数组中创建一个链表。当我尝试将arrTab-> h_table [index]的指针更新为newNode的地址时,该地址指向newNodes地址。但是当我尝试添加到数组中存在的列表时,指针始终指向NULL而不是内存中的先前值。基本上,链表的arrTab-> h_table [index]头部不会更新为newNode的地址。
typedef struct node {
struct node* next;
int hash;
s_type symbol;
} node_t;
struct array {
int cap;
int size;
n_type** h_table;
};
int add_to_array (array* arrTab, const char* name, int address) {
if(s_search(arrTab, name, NULL, NULL) == NULL){
s_type *symbol = (s_type*) calloc(1, sizeof(s_type));
symbol->name = strdup(name);
symbol->addr = addr;
n_type *newNode = (n_type*) calloc(1, sizeof(n_type));
newNode->next = NULL;
newNode->hash = nameHash(name);
newNode->symbol = *symbol;
int index = newNode->hash % arrTab->cap;
if(arrTab->h_table[index] == NULL){
arrTab->h_table[index] = newNode;
} else {
newNode->next = arrTab->h_table[index];
arrTab->h_table[index] = newNode;
}
//
arrTab->size++;
return 1;
}
return 0;
}
struct node* s_search (array* arrTab, const char* name, int* hash, int* index) {
int hashVal = nameHash(name);
hash = &hashVal;
int indexVal = *hash % arrTab->cap;
index = &indexVal;
s_type *symCopy = arrTab;
while (symCopy->h_table[*index] != NULL){
if(*hash == symCopy->h_table[*index]->hash){
return symCopy->h_table[*index];
}
symCopy->h_table[*index] = symCopy->h_table[*index]->next;
}
return NULL;
}
我不能肯定地说为什么指针总是指向NULL;没有足够的代码。考虑发布MCVE。
但是,发布的代码几乎没有问题需要解决。
首先,它像没有明天一样泄漏记忆:
symbol_t *symbol = (symbol_t*) calloc(1, sizeof(symbol_t));
分配一些内存,和
newNode->symbol = *symbol;
将该内存的内容复制到新位置。分配的内存仍然存在,并在函数返回后继续存在 - 但是没有办法达到它。我强烈建议不要分配symbol
,并直接与newNode->symbol
合作:
newNode->symbol.name = strdup(name);
newNode->symbol.addr = addr;
hash
的index
和symbol_search
参数似乎被计划为out参数。在这种情况下,请注意hash = &hashVal;
和index = &indexVal;
的结果对调用者是不可见的。你可能意味着*hash = hashVal
和*index = indexVal
。
最大的问题来自sym_table_t *symCopy = symTab;
。
symTab
是一个指针。它指向一个实际的符号表,一个很大的记忆。在任务完成后,symCopy
指向同一块记忆。意思就是
symCopy->hash_table[*index] = symCopy->hash_table[*index]->next;
修改那段记忆。搜索完成后,hash_table[index]
与搜索前的不一样。这可能是你问题的根源。无论如何,请考虑
node_t * cursor = symTab->hash_table[*index];
并使用此光标。
作为旁注,搜索条件*hash == symCopy->hash_table[*index]->hash
很奇怪。给定链表中的每个节点都具有相同的哈希值(检查如何添加它们)。即使名称不同,第一个节点也会产生匹配。