2016-03-14 66 views
-3

你好我正在做一个项目,我必须实现一个基于散列函数存储单词的hashTable。在压力测试我得到的malloc():内存破坏malloc():在字符串连接上的内存损坏

的哈希表

hashTable = (char**)malloc(hashSize[0] * sizeof(char*)); 

这最初的声明是我写的单词添加到HASHSIZE的哈希表中的功能:

void addWord(char** hashTable, unsigned int hashSize, const char* word) { 

    int bucketIndex = hash(word, hashSize); 
    //printf("Word to add = %s, bucket = %d, hashTable size = %d\n", word, bucketIndex, hashSize); 
    if(hashTable[bucketIndex] == NULL) { 
     hashTable[bucketIndex] = (char*)malloc(strlen(word) * sizeof(char)); 
     strcpy(hashTable[bucketIndex], word); 
     return; 
    } 
    /* checks for duplicats */ 
    int exists = 0;  
    char* heyStack = (char*)malloc(strlen(hashTable[bucketIndex])); 
    memcpy(heyStack, hashTable[bucketIndex], strlen(hashTable[bucketIndex])); 
    char* token = strtok(heyStack, " "); 
    while(token) { 
     if(strcmp(token, word) == 0) { 
      exists = 1; 
      break; 
     } 
     token = strtok(NULL, " "); 
    } 
    /* end check for duplicates */ 
    if(exists == 0) { 
     size_t bucketSize = strlen(hashTable[bucketIndex]); 
     hashTable[bucketIndex] = (char*)realloc(hashTable[bucketIndex], bucketSize + strlen(word) + 2); 
     memcpy(hashTable[bucketIndex] + bucketSize, " ", 1); 
     memcpy(hashTable[bucketIndex] + bucketSize + 1, word, strlen(word) + 1); 

    } 
} 

我有一个压力测试,增加了20k字的表,它总是打破同一个词(没有10k东西)

任何想法,我在做什么错?

Tyvm

+1

'malloc(strlen(word)* sizeof(char)'后跟'strcpy'。您必须为'nul'终止符再分配一个字节。 –

+0

增加了空终止符的额外字节。问题依然存在 – user1840302

+0

I我不知道你用'heyStack'做了什么,你根据'strlen'(没有额外的字节)分配内存,但是你使用'memcpy'。是否会留下字符串终止符? –

回答

0

你必须把它传递到处理字符串处理功能,如strlen()strtok()之前终止“弦”。

  • 分配字符串的大小和一个字节以终止空字符。
  • 通过添加空字符来终止“字符串”。

注:

更正代码:

void addWord(char** hashTable, unsigned int hashSize, const char* word) { 

    int bucketIndex = hash(word, hashSize); 
    //printf("Word to add = %s, bucket = %d, hashTable size = %d\n", word, bucketIndex, hashSize); 
    if(hashTable[bucketIndex] == NULL) { 
     size_t wordSize = strlen(word); 
     hashTable[bucketIndex] = malloc(wordSize + 1); /* size +1 */ 
     memcpy(hashTable[bucketIndex], word, wordSize + 1); /* why did you use strcpy() only in here? */ 
     return; 
    } 
    /* checks for duplicats */ 
    int exists = 0; 
    size_t dataSize = strlen(hashTable[bucketIndex]); 
    char* heyStack = malloc(dataSize + 1); /* size +1 */ 
    memcpy(heyStack, hashTable[bucketIndex], dataSize + 1); /* size +1 */ 
    char* token = strtok(heyStack, " "); 
    while(token) { 
     if(strcmp(token, word) == 0) { 
      exists = 1; 
      break; 
     } 
     token = strtok(NULL, " "); 
    } 
    /* end check for duplicates */ 
    if(exists == 0) { 
     size_t bucketSize = strlen(hashTable[bucketIndex]); 
     size_t wordSize = strlen(word); 
     hashTable[bucketIndex] = realloc(hashTable[bucketIndex], bucketSize + wordSize + 2); 
     memcpy(hashTable[bucketIndex] + bucketSize, " ", 1); 
     memcpy(hashTable[bucketIndex] + bucketSize + 1, word, wordSize + 1); 
    } 
    free(heyStack); /* do free what you allocated */ 
} 

,如果你添加一些代码来检查,如果malloc()realloc()是成功,则该代码会更好。