C内存管理 - >哈希

时间:2015-01-04 21:18:33

标签: c algorithm hash hashtable

我不知道我的问题是否是内存泄漏,或者我没有以正确的方式访问哈希表。

我的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];

我的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;
}

我的输出是

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

如果有人能解释我,请提前谢谢。

2 个答案:

答案 0 :(得分:1)

valores是一个数组。您正在向Info typedef插入已void *的{​​{1}}。你需要解决这些问题。

答案 1 :(得分:0)

您的malloc不是必要的,之所以不明显,是因为它被您typedef HashTable的方式所隐藏,永远不会被隐藏这样做,以下代码按照您的预期进行操作

#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;
}

typedef的{​​{1}}使得很难知道如何处理HashTable类型变量,这对HashTable并不是很好用。

无论条件如何,第二个typedef都将被执行

printf

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

{