현재 C의 일치 색인 프로그램에서 작업 중입니다. 프로그램을 실행하려고하면 오류가 발생합니다.C 이중 자유 또는 손상 오류 프로그램
이 내 C 프로그램입니다 :
#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;
}
그리고 그 다음은 내가지고있어 오류입니다 :
* glibc에 감지 * 일치 : 더블 무료 또는 손상 (! 이전) : 0x0000000001060f010
이 오류가 발생할 수있는 원인을 확신 할 수 없습니다. 이것에 대한 어떤 도움이라도 좋을 것입니다.
이중 자유 및 손상을 감지하기 위해 원하는 도구를 사용하십시오. 가장 인기있는 것은 아마도 'valgrind'입니다. –
'fclose'를 두 번 호출합니다. – Dai
입력 파일이 텍스트 파일이면'strlen (file_contents)'는'input_file_size'와 같습니다. 즉, 함수'empty'는 조금 의심스러워 보입니다. 당신이 그걸로 무엇을 이루기를 바랐습니까? –