希望可以帮助我解决这个问题。
我正在使用.txt文件,其中包含单词列表。我阅读了所有单词,并将它们放入名为StopWords的结构中。
我的问题是,当我使用valgrind时,strtok行出现了错误。
==11976== Invalid write of size 8
==11976== at 0x4027FC: loadStopWords (functions.c:735)
==11976== by 0x4028D0: main (search.c:23)
==11976== Address 0x52067d8 is 0 bytes after a block of size 2,552 alloc'd
==11976== at 0x4C2DBF6: malloc (vg_replace_malloc.c:299)
==11976== by 0x4026FA: loadStopWords (functions.c:704)
==11976== by 0x4028D0: main (search.c:23)
我真的不知道这里发生了什么。
StopWords* loadStopWords(char* pathStopWordsFile, code *statusCode)
{
StopWords* sWords;
FILE *file;
int size;
char* dataFileChar;
int wordsNumber = 1;
int wordLen = 0;
char*token;
int count = 1;
file = fopen (pathStopWordsFile, "r" );
if (file==NULL)
{
*statusCode = ERR_FILE_NOT_FOUND;
exit(1);
}
else
{
*statusCode = OK;
}
fseek(file,0,SEEK_END);
size = ftell(file);
rewind(file);
dataFileChar = (char*)malloc(sizeof(char)*(size+1));
fread(dataFileChar,sizeof(char), size, file);
dataFileChar[size] = '\0';
fclose(file);
for (int i = 0; i < size ; ++i)
{
if(dataFileChar[i] == '\n')
{
wordsNumber++;
}
}
sWords = (StopWords*)malloc(sizeof(StopWords));
sWords->words = (char**)malloc(sizeof(char*)*wordsNumber);
wordsNumber = 1;
for (int i = 0; i < size; ++i)
{
if(dataFileChar[i] == '\n')
{
wordsNumber++;
sWords->words[wordLen] = (char*)malloc(sizeof(char)*wordLen);
wordLen = 0;
}
else if(dataFileChar[i] != '\n' && dataFileChar[i] != '\r')
{
wordLen++;
}
}
wordLen = 0;
token = strtok(dataFileChar, "\n");
sWords->words[0] = token;
removeInvisibleCharacter(sWords->words[0]);
while( token )
{
token = strtok(NULL, "\n");
sWords->words[count] = token;
removeInvisibleCharacter(sWords->words[count-1]);
count++;
}
sWords->numberOfStopWords = wordsNumber;
free(dataFileChar);
return sWords;
}
我也有程序的主程序,如果有人需要结构或主程序,请问我。
最佳答案
让我给您几个提示:
1)
sWords->words[wordLen] = (char*)malloc(sizeof(char)*wordLen);
使用
words[wordLen]
没有任何意义。您应该通过当前正在处理的单词的数量来建立索引。我认为这是正确的:sWords->words[wordsNumber++] = (char*)malloc(sizeof(char)*wordLen);
我们使用后缀增量将其值用作索引后增加一,因此您可以删除前一行(
wordsNumber++;
)。2)
由于C数组索引基于零,因此将
wordsNumber = 1;
从1)中提到的for
循环之前更改为wordsNumber = 0;
。3)
removeInvisibleCharacters()
的作用是什么?源代码(或至少是签名)是什么?由于可能会发现其他问题,因此我将在此处添加更多要点。