for example make your HashTableLookUp return list_ptr, in this case you will have no problem to update list_ptr->value
for example make your HashTableLookUp return list_ptr, in this case you will have no problem to update list_ptr->value
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
if it returns an address to the old value then changing that address wouldn't work right because the list->value still points to the old address
deleted
Last edited by -EquinoX-; 03-29-2008 at 08:40 AM.
one simple question, do I have to include cstr.h or cstr.c in hash.c in order to use the StringHash that I created in the cstr module?
probably cstr.h - look at your warnings about explicitely defined functions to be sure
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I tried cstr.h and it doesn't works, I tried cstr.c and it works
You should not include one c file into another
but you should compile them together
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
okay I got it
check out my new version of insert, do you think it's perfect now??
StringDestruct basically just removes old value from the heap space, basically it just calls free to the pointer of temp-> value at that time after it free's the old value it creates a copy of the new value in the heap space and then have temp->value to that addressCode:boolean HashTableInsert(HashTable *this, const char* key, const char* value) { list_t *new_list, *temp; int_u4 hashval = StringHash(key) % this->length; /* Attempt to allocate memory for list */ if ((new_list = malloc(sizeof(list_t))) == NULL) outOfMemory_(__LINE__, __FILE__, "struct node creation"); /* Does item already exist? */ if (HashTableContains(this, key) == true){ for (temp = this->table[hashval]; temp!= NULL; temp = temp->next){ if (strcmp(key, temp->key) == 0){ StringDestruct(temp->value); temp->value = StringConstruct(value); return false; } } } else{ /* Insert into list */ new_list->key = StringConstruct(key); new_list->value = StringConstruct(value); new_list->next = this->table[hashval]; this->table[hashval] = new_list; this->length = this->length + 1; return true; } }
1. new_list is used only in else - but allocated anyway - you get memory leak in the if case
2. if this->table[hashval] was not NULL in the else case - you effectivly loosing this value
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
so I just should use either new_list or temp for both??
so you should allocate new_list only in the else branch - becase when the key is found - you do not need to create the new node - instead you are replacing value of the existing node
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I see and that would solve problem no.1 that you mentioned above right? but how about number 2? I don't get what you mean
what about now:
Code:boolean HashTableInsert(HashTable *this, const char* key, const char* value) { list_t *temp; int_u4 hashval = StringHash(key) % this->length; /* Does item already exist? */ if (HashTableContains(this, key) == true){ for (temp = this->table[hashval]; temp!= NULL; temp = temp->next){ if (strcmp(key, temp->key) == 0){ StringDestruct(temp->value); temp->value = StringConstruct(value); return false; } } } else if (temp == NULL){ /* Insert into list */ list_t *new_list; if ((new_list = malloc(sizeof(list_t))) == NULL) outOfMemory_(__LINE__, __FILE__, "struct node creation"); new_list->key = StringConstruct(key); new_list->value = StringConstruct(value); new_list->next = this->table[hashval]; this->table[hashval] = new_list; this->length = this->length + 1; return true; } }
Last edited by -EquinoX-; 03-29-2008 at 10:59 AM.
in your else branch you could have already several links connected to the same have entry
so the list could be not empty.
How would you go about adding new node to the list if you know that the new node is pointed by new_list pointer and list head is stored in the this->table[hashval] pointer?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler