-2

Currently working on a concordance program in C. When I try to run the program though, I get an error.

This is my 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 the table
}

int insert(const void *bb, const void *cc) {
    return strcmp(*(const char **)bb, *(const char **)cc);
}

void empty(char *Table[]) {
    strcat(Table,"NULL"); // Empties the table
}

int main(int argc, char *argv[]){


    if(argc!=3){
        printf("ERROR: Usage: concordance table_size"); // Errors due to not enough variables (Should be tablesize and file name)
    } else {

        FILE *fp;  //This block opens the file user has inputted and makes the string "File_contents" set to the 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 all words to lower case
        }

        i=0;
        word = strtok(file_contents, " ,.-:;?!"); //Chars which 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
    }
    return 0;
}

And the following is the error I'm getting:

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

Not sure what could be causing this error to happen. Any help on this would be great though.

indiv
  • 17,306
  • 6
  • 61
  • 82
user3434743
  • 155
  • 2
  • 9
  • 3
    Use whatever tool you like for detecting double frees and corruption. The most popular is probably `valgrind`. – David Schwartz Oct 21 '14 at 23:18
  • 2
    You're calling `fclose` twice. – Dai Oct 21 '14 at 23:19
  • 1
    If your input file is a text file, then `strlen(file_contents)` equals `input_file_size`. That being said, function `empty` looks a bit suspicious. Would you mind sharing with us what were you hoping to achieve with it? – barak manos Oct 22 '14 at 08:34
  • Run it with a debugger who will inform you where that double free occurs. And explain us what your program is supposed to to (best with an example of input and expected output). – Jabberwocky Oct 22 '14 at 09:59
  • Possible duplicate of [How to track down a double free or corruption error in C++ with gdb](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error-in-c-with-gdb) – Raedwald Dec 06 '18 at 09:06

2 Answers2

0

You aren't calling fclose() twice. Which I suppose in turn might call free() internally. Remove the fclose() at the end of the program.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-2

you are passing NULL as argument to strtok function. I think this may cause the problem

Saravanan
  • 153
  • 6