malloc():字符串连接

时间:2016-03-14 11:34:03

标签: c memory-corruption

你好我正在做一个项目,我必须实现一个hashTable,它存储基于哈希函数的单词。在压力测试中我得到malloc():内存损坏

hashTable的初始声明

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

这是我写的用于向hashSize的hashTable添加单词的函数:

    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 个答案:

答案 0 :(得分: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()是否成功,则此代码会更好。