2016-03-14 110 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()是成功,則該代碼會更好。