Skip to content
Browse files

doesnt leak! but full of valgrind errors :(

  • Loading branch information...
1 parent c1349ba commit 7a166db04c75b9125275d13f005d775f6e76289a @capotej committed Jul 1, 2012
Showing with 25 additions and 23 deletions.
  1. +1 −1 ourlib/Makefile
  2. +11 −8 ourlib/hash.c
  3. BIN ourlib/main
  4. +13 −14 ourlib/main.c
  5. BIN ourlib/main.dSYM/Contents/Resources/DWARF/main
View
2 ourlib/Makefile
@@ -7,4 +7,4 @@ clean:
rm -f main
build:
- gcc -g -o main main.c && valgrind --dsymutil=yes ./main
+ gcc -g -o main main.c && valgrind --leak-check=full --track-origins=yes --dsymutil=yes ./main
View
19 ourlib/hash.c
@@ -54,22 +54,21 @@ void *hash_get(struct Hash *hsh, void *key) {
void *spot = array_get(hsh->arr, k);
struct Node *node = ((struct Node *)(spot));
- int keylen = strlen(key) + 6;
+ int keylen = strlen((char *)key) + 6;
char *needle = malloc(sizeof(char) + keylen + 1);
*needle = 0;
strcat(needle, key);
strcat(needle, "::::::");
- char *result = 0;
+ char *result = NULL;
-
- if(strstr(&node->data, &needle) != NULL){
- result = node->data;
+ if(strstr(node->data, needle) != NULL){
+ result = node->data;
}
while(node->next != NULL){
node = node->next;
- if(strstr(&node->data, &needle)){
+ if(strstr((char *)node->data, needle)){
result = node->data;
}
}
@@ -79,13 +78,17 @@ void *hash_get(struct Hash *hsh, void *key) {
free(needle);
return formatted;
} else {
+ free(needle);
return NULL;
}
}
void hash_put(struct Hash *hsh, void *key, void *value) {
printf("hash_put %s -> %s\n", (char *)key, (char *)value);
void *exists = hash_get(hsh, key);
+ if(exists != NULL){
+ printf("LKEY ASDAS");
+ }
int len = hsh->max_size;
int k = hash(key) % len;
hsh->keys++;
@@ -102,9 +105,9 @@ void hash_put(struct Hash *hsh, void *key, void *value) {
hsh->map[k] = hsh->map[k] + 1;
node_push(((struct Node *)(spot)), encoded);
} else {
- printf(" map key found: key does exist, map level is %d\n", hsh->map[k]);
+ printf(" map key found: key does exist, map level is %d ", hsh->map[k]);
struct Node *found_node = node_get(((struct Node *)(spot)), hsh->map[k] - 1);
- printf(" is %s", found_node->data);
+ printf(" is %s \n", found_node->data);
found_node->data = encoded;
}
}
View
BIN ourlib/main
Binary file not shown.
View
27 ourlib/main.c
@@ -38,20 +38,19 @@ int main() {
struct hash *hsh = new_hash(1);
- //hash_put(hsh, "k1", "one");
-
- // hash_put(hsh, "k2", "two");
- // hash_put(hsh, "k1", "changed");
- // hash_put(hsh, "k1", "another");
- // hash_put(hsh, "k2", "dos");
-
- //void* y = hash_get(hsh, "k1");
- // void* z = hash_get(hsh, "k2");
- // void* yz = hash_get(hsh, "k3");
-
- //printf(" one should be %s\n", ((char *)y));
- // printf(" dos should be %s\n", (char *)z);
- // printf(" null should be %s\n", (char *)yz);
+ hash_put(hsh, "k1", "one");
+ hash_put(hsh, "k2", "two");
+ hash_put(hsh, "k1", "changed");
+ hash_put(hsh, "k1", "another");
+ hash_put(hsh, "k2", "dos");
+
+ void* y = hash_get(hsh, "k1");
+ void* z = hash_get(hsh, "k2");
+ void* yz = hash_get(hsh, "k3");
+
+ printf(" another should be %s\n", ((char *)y));
+ printf(" dos should be %s\n", (char *)z);
+ printf(" null should be %s\n", (char *)yz);
free_hash(hsh);
View
BIN ourlib/main.dSYM/Contents/Resources/DWARF/main
Binary file not shown.

0 comments on commit 7a166db

Please sign in to comment.
Something went wrong with that request. Please try again.