C Program double free or corruption error -



C Program double free or corruption error -

currently working on concordance programme in c. when seek run programme though, error.

this c program:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void print(char table, int n) { printf("%d: ", n+1); // prints table } int insert(const void *bb, const void *cc) { homecoming strcmp(*(const char **)bb, *(const char **)cc); } void empty(char *table[]) { strcat(table,"null"); // empties table } int main(int argc, char *argv[]){ if(argc!=3){ printf("error: usage: concordance table_size"); // errors due not plenty variables (should tablesize , file name) } else { file *fp; //this block opens file user has inputted , makes string "file_contents" set file's contecnts fp = fopen(argv[2],"r"); char *file_contents; long input_file_size; fseek(fp, 0, seek_end); input_file_size = ftell(fp); rewind(fp); file_contents = malloc((input_file_size + 1) * (sizeof(char))); fread(file_contents, sizeof(char), input_file_size, fp); fclose(fp); file_contents[input_file_size] = 0; char *word, *words[strlen(file_contents)/2+1]; int i, n; for(i=0;file_contents[i];i++){ file_contents[i]=tolower(file_contents[i]); //converts words lower case } i=0; word = strtok(file_contents, " ,.-:;?!"); //chars signal end of word while(word != null) { words[i++] = word; word = strtok(null, " ,.-:;?!"); } n = i; qsort(words, n, sizeof(*words), insert); for(i=0; i<n; ++i){ print(words[i],i); printf("%s\n", words[i]); } empty(words); fclose(fp); // closes open file } homecoming 0; }

and next error i'm getting:

* glibc detected * concordance: double free or corruption (!prev): 0x0000000001060f010

not sure causing error happen. help on great though.

you aren't calling fclose() twice. suppose in turn might phone call free() internally. remove fclose() @ end of program.

c

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -