这意味着什么?对于大文件,我应该如何修复它?(malloc\u错误\u中断)

这意味着什么?对于大文件,我应该如何修复它?(malloc\u错误\u中断),c,C,问题: 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 <stdl

问题:

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; }
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.
这个代码示例搜索单词并计算单词数,对于包含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; }
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.

代码示例:

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; }
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.
输出:

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; }
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.
插入文件名:(例如test.txt)

test.txt

总字数=120

唯一字数=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 */
注意:试图释放
单词[uniqueWordCount]
生成错误,因为
uniqueWordCount
超过了最后分配的指针)


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

您能解释一下错误消息中不清楚的地方吗?听起来很清楚,对我很有帮助。您似乎正在释放一个未分配的指针,可以使用断点对其进行调试。如果输入中有任何长度超过99个字符的连续非空白字符字符串,您可能会溢出
temp
,这可能会覆盖
words
数组中的指针并导致此问题。正如目前编写的,您的程序容易受到缓冲区溢出的影响。检查您的数据是否有问题。
free(words[uniqueWordCount])
--您试图在N个唯一单词的列表后释放值N次。感谢您的回答,非常感谢。:)当然,我们都去过。。祝你的编码好运!顺便说一下,这也是一个老师的例子。我觉得这个错误是因为没有给出作业的答案。。。不过,这是一个糟糕的示例,感谢您强调错误,因为释放正确的内存地址很重要。