gpt4 book ai didi

c - 这是什么意思,我应该如何为大文件解决这个问题? (malloc_error_break)

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:36 26 4
gpt4 key购买 nike

问题:

此代码示例搜索单词并计算单词数,以及具有 1000 行的较大 txt 文件的唯一单词数我收到错误,有什么想法吗?

错误:

week7_14(43430,0x1087085c0) malloc: *** error for object 0x696c617274737561: pointer being freed was not allocated
week7_14(43430,0x1087085c0) malloc: *** set a breakpoint in malloc_error_break to debug

代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
#define MAXWORDS 1000

int findWord(char *word[], char *temp, int index) {
for (int i=0; i<index; i++)
{
if (strcmp(word[i], temp) == 0) // found the word
return 1;
}
return 0; // cannot find word }

int main(int argc, char **argv) {
char filename[MAXLEN];
char *words[MAXWORDS] = {NULL};
char temp[MAXLEN];
int wordCount = 0;
int uniqueWordCount = 0;
int i = 0;
// read in the filename
printf("insert a file name: (eg. test.txt)\n");
scanf("%s", filename);
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Cannot open file %s\n", filename);
return 1;
}

while ((fscanf(fp, "%s", temp) == 1) && uniqueWordCount<MAXWORDS)
{
wordCount++; // update total number of words
// find word in words array
if (!findWord(words, temp, uniqueWordCount))
{
words[uniqueWordCount] = calloc(strlen(temp)+1,
sizeof(char));
if (words[uniqueWordCount] == NULL)
{
printf("calloc failed to allocate memory\n");
return 1;
}
strcpy(words[uniqueWordCount], temp);
uniqueWordCount++; // update number of unique words
}
}
fclose(fp);
while (i<uniqueWordCount)
{
free(words[uniqueWordCount]);
i++;
}
printf("Total number of words = %d\n", wordCount);
printf("Number of unique words = %d\n", uniqueWordCount);
return 0; }

有效的示例文本文件 (test.txt):

Any girl jumped over one boy.
Some car skipped to some boy.
One town drove over the town.
Any town ran under some dog.
Some girl drove to a town.
The boy walked under any town.
A town jumped over any car.
Any boy jumped from a car.
A dog ran over a boy.
A girl ran to some car.
A car ran under the girl.
The car ran on any town.
One dog walked under any dog.
A car jumped on some town.
A boy ran to a boy.
The dog drove over a boy.
A boy jumped over the car.
Some car drove on some girl.
One boy drove under some girl.
A girl walked over some dog.

输出:

insert a file name: (eg. test.txt)

test.txt

Total number of words = 120

Number of unique words = 30

最佳答案

您有几个问题需要解决,(1),您缺少右大括号:

int findWord(char *word[], char *temp, int index) {
for (int i=0; i<index; i++)
{
if (strcmp(word[i], temp) == 0) // found the word
return 1;
}
return 0; // cannot find word }
} /* missing closing brace */

(2) 你有一个错字并且试图释放 words[uniqueWordCount] 而不是 words[i],例如

    for (i = 0; i<uniqueWordCount; i++) /* loop over each word in words */
free(words[i]); /* free words[i], not words[uniqueWordCount] */

(注意尝试释放 words[uniqueWordCount] 会产生错误,因为 uniqueWordCount 是最后分配的指针之后的一个)

最后,您应该使用 int main (void),因为既没有使用 int argc 也没有使用 char **argv

关于c - 这是什么意思,我应该如何为大文件解决这个问题? (malloc_error_break),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com