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
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.
Comments