1

Im trying create a linked list in an array of nodes. When I try to update the pointer for arrTab->h_table[index] to the address of newNode, The address points to newNodes address. But when I try to add to a list that exists in the array, the pointer always points to NULL instead of the previous value in memory. Basically the arrTab->h_table[index] head of the linked list does not update to the address of 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;
}
1
  • Moderator Note: Please do not vandalize your posts. Once you post a question, they belong to the site and its users. Even if it is no longer useful to you, it might be helpful to someone in the future. The answerers would have also put an effort in writing their answer, which would no longer be useful if you have removed the content from the post. Also, note that by posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted. Commented Feb 18, 2019 at 7:10

1 Answer 1

4

I cannot say for sure why the pointer always points to NULL; there is not enough code. Consider posting an MCVE.

The posted code however presents few problems to address.

First, it leaks memory like there is no tomorrow:

symbol_t *symbol = (symbol_t*) calloc(1, sizeof(symbol_t));

allocates some memory, and

newNode->symbol = *symbol;

copies the contents of that memory to the new location. The memory allocated still exists, and continues to exist after the function returns - but there's no way to get to it. I strongly recommend to not allocate symbol, and work directly with newNode->symbol:

newNode->symbol.name = strdup(name);
newNode->symbol.addr = addr;

The hash and index parameters to symbol_search seem to be planned as an out parameters. In that case, notice that the results of hash = &hashVal; and index = &indexVal; are invisible to the caller. You likely meant *hash = hashVal and *index = indexVal.

The biggest problem comes with sym_table_t *symCopy = symTab;.

symTab is a pointer. It points to an actual symbol table, a big piece of memory. After the assignment, symCopy points to the same piece of memory. Which means that

symCopy->hash_table[*index] = symCopy->hash_table[*index]->next;

modifies that piece of memory. Once the search is completed, the hash_table[index] is not the same as it was before the search. This could be a root of your problem. In any case, consider

node_t * cursor = symTab->hash_table[*index];

and work with this cursor instead.

As a side note, a search condition *hash == symCopy->hash_table[*index]->hash is strange. Every node in a given linked list has the same hash (check how you add them). The very first node would produce a match, even if the names are different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.