-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
'malloc(strlen(word)* sizeof(char)'后跟'strcpy'。您必须为'nul'终止符再分配一个字节。 –
增加了空终止符的额外字节。问题依然存在 – user1840302
I我不知道你用'heyStack'做了什么,你根据'strlen'(没有额外的字节)分配内存,但是你使用'memcpy'。是否会留下字符串终止符? –