What does this mean and how should I fix this for big files? (malloc_error_break)

jaycodez

Problem:

This code example searches for words and counts the number of words, and the number of unique words for larger txt files with 1000s of lines I get an error, any ideas?

ERROR:

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

Code Example:

#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; }

Example text file that works (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.

Output:

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

test.txt

Total number of words = 120

Number of unique words = 30

David C. Rankin

You have several problems to address, (1), you are missing the closing brace in:

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) you have a typo and are attempting to free words[uniqueWordCount] instead of words[i], e.g.

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

(note: attempting to free words[uniqueWordCount] generated your error as uniqueWordCount is one past the last allocated pointer)

Last, you should use int main (void) since neither int argc or char **argv are used.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What does this warning mean and what should I do to fix it?

What does "error: '.class' expected" mean and how do I fix it

what does this error mean, and how to fix it?

What does "Overflow evaluating the requirement" mean and how can I fix it?

What does this mean? How do I fix it? Fatal Exception:main

What exactly does my logcat mean, and how can I fix it?

What does this program mean by Unknown Source and how do I fix it?

"local variable 'e' referenced before assignment" what does this error mean? How do I fix this error?

Error: No value given for one or more required parameters. What does this mean? How do I fix?

'No module named 'deploy' : What does this error mean and how do I fix it?

What does this error mean Unsupported class file major version 56 and how do I fix it?

What does the error "list indices must be integers or slices, not str" mean and how can I fix it?

C++ - What does "Incomplete type not allowed" error mean, and how can I fix it?

What does 'index 0 is out of bounds for axis 0 with size 0' mean and how can I fix this error?

What does Bus error: 10 mean? And how to fix this error

What does Grub error "no such device: /.disk/info" mean, and how to fix it?

What does MissingManifestResourceException mean and how to fix it?

how to fix the error on the Round, what should i change the line to?

What does Error:(13) Error: The <receiver> element must be a direct child of the <application> element [WrongManifestParent] mean and how do i fix it?

Android gradle build Error "finished with non-zero exit value 42" , what does it mean and how do i fix it?

What does the rust-analyzer error "could not resolve macro `$crate::format_args`" mean and how do I fix it?

What does this python2 error mean, and what should I do about it?

What does 'Unsupported major.minor version 52.0' mean, and how do I fix it?

What does mean \g at the end of a MySQL statement? And how do I fix queries not running?

What does "RuntimeWarning: Enable tracemalloc to get the object allocation traceback" mean? I know how to fix, looking for the meaning

What does the warning "doc comment not used by rustdoc" mean and how do I fix it?

How to fix the following errors when I typed in "heroku logs --tail" on my terminal ?? what does it mean?

In MySQL what does "Overhead" mean, what is bad about it, and how to fix it?

What does the following code mean??And how should I understand the syntax of the function body?