Duozhasht
Duozhasht

Reputation: 9

C Memory Management -> Hash

I don't know if my problem is a memory leak, or i'm not acessing the hashtable in the correct way.

My hash.h

#define HASHSIZE 31
#define EMPTY   ""
#define DELETED "-"

typedef char KeyType[9];

typedef void *Info;

typedef struct entry
{
    KeyType key;
    Info info;
}Entry;

typedef Entry HashTable[HASHSIZE];

My hash.c

int Hash(KeyType k){
    return atoi(k)%HASHSIZE;
}

void InitializeTable(HashTable t){
    for(int i=0; i < HASHSIZE; i++){
        strncpy(t[i].key,EMPTY,9);
    }
}

void ClearTable(HashTable t){
    InitializeTable(t);
}

void InsertTable_LP(HashTable t, KeyType k, Info i){
    int a = 0;
    int hash = Hash(k);
    while((a<HASHSIZE) 
            && strcmp(t[hash].key,EMPTY)!=0
                && strcmp(t[hash].key,DELETED)!=0  ){
        hash = (hash + 1) % HASHSIZE;
        a++;
    }

    strncpy(t[hash].key,k,9);
    t[hash].info = i;
    printf("Value of info is %d\n",(int)t[hash].info);

}

int RetrieveTable_LP(HashTable t, KeyType k){
    int a=0;
    int hash = Hash(k);

    while(a<HASHSIZE 
            && strcmp(t[hash].key,k)!=0
                && strcmp(t[hash].key,EMPTY)!=0){
        hash=(hash+1) % HASHSIZE;
        a++;    
    }

    if(strcmp(t[hash].key,k)==0)
        return hash;
    return -1;

}


int main(){
    HashTable *t = malloc(HASHSIZE*sizeof(Entry));
    int valores[] = {1,2,3,4,5,6,7,8,9};

    ClearTable(*t);
    InsertTable_LP(*t,"1",valores);
    InsertTable_LP(*t,"2",valores+1);
    InsertTable_LP(*t,"3",valores+2);
    InsertTable_LP(*t,"4",valores+3);
    InsertTable_LP(*t,"5",valores+4);

    int pos = RetrieveTable_LP(*t,"2");
    if(pos==-1){
        printf("Error\n");
    }
    else
        printf("Position %d\n",pos);
        printf("okay %d\n",(int)t[pos]->info);

    printf("asdasdas\n");

    return 1;
}

My output is

Value of info is 1537727040
Value of info is 1537727044
Value of info is 1537727048
Value of info is 1537727052
Value of info is 1537727056
Position 2
okay 0

If anyone could explain me, thanks in advance.

Upvotes: 0

Views: 139

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

Your malloc is not necessary the reason why that wasn't obvious is because it was hidden by the way you typedefd the HashTable, don't ever do that, the following code works as you expected yours to do

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HASHSIZE 31
#define EMPTY   ""
#define DELETED "-"

typedef char KeyType[9];

typedef void *Info;

typedef struct entry
{
    KeyType key;
    Info info;
}Entry;

typedef Entry HashTable[HASHSIZE];

int Hash(KeyType k){
    return atoi(k)%HASHSIZE;
}

void InitializeTable(HashTable t) {
    int i;
    for(i=0; i < HASHSIZE; i++) {
        strncpy(t[i].key, EMPTY, 9);
    }
}

void ClearTable(HashTable t) {
    InitializeTable(t);
}

void InsertTable_LP(HashTable t, KeyType k, Info i){
    int a = 0;
    int hash = Hash(k);
    while((a<HASHSIZE) && strcmp(t[hash].key, EMPTY) !=0 && strcmp(t[hash].key, DELETED) !=0  ) {
        hash = (hash + 1) % HASHSIZE;
        a++;
    }
    strncpy(t[hash].key, k, 9);

    t[hash].info = i;

    printf("Value of info is %p\n", t[hash].info);

}

int RetrieveTable_LP(HashTable t, KeyType k){
    int a=0;
    int hash = Hash(k);

    while(a<HASHSIZE 
            && strcmp(t[hash].key,k)!=0
                && strcmp(t[hash].key,EMPTY)!=0){
        hash=(hash+1) % HASHSIZE;
        a++;    
    }

    printf("%s, %s\n", t[hash].key, k);
    if(strcmp(t[hash].key, k)==0)
        return hash;
    return -1;

}


int main(){
    /* 
     * You don't need to malloc, since HashTable is an array,
     * and it does not need to be a pointer, since it decays
     * to one when passed as such.
     */
    HashTable t;// = malloc(HASHSIZE * sizeof(Entry));
    int valores[] = {1,2,3,4,5,6,7,8,9};

    ClearTable(t);

    InsertTable_LP(t,"1",valores);
    InsertTable_LP(t,"2",valores+1);
    InsertTable_LP(t,"3",valores+2);
    InsertTable_LP(t,"4",valores+3);
    InsertTable_LP(t,"5",valores+4);

    int pos = RetrieveTable_LP(t, "2");
    if(pos==-1) {
        printf("Error\n");
    }
    else
    {
        printf("Position %d\n",pos);
        printf("okay %p\n", t[pos].info);
    }

    printf("asdasdas\n");

    return 1;
}

your typedef of the HashTable makes it hard to know what to do with a HashTable type variable, that is not a very good use of typedef.

Also the second printf will be executed regardless of the condition

else
    printf("Position %d\n",pos);
    printf("okay %d\n",(int)t[pos]->info);

you need to add {

else
{
    printf("Position %d\n",pos);
    printf("okay %d\n",(int)t[pos]->info);
}

Upvotes: 0

unxnut
unxnut

Reputation: 8839

valores is an array. You are inserting Info which has been typedefed to void *. You need to fix those things.

Upvotes: 1

Related Questions