目前正在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'。 – 2014-10-21 23:18:39
你在调用'fclose'两次。 – Dai 2014-10-21 23:19:48
如果你的输入文件是一个文本文件,那么'strlen(file_contents)'等于'input_file_size'。话虽如此,功能'空'看起来有点可疑。你介意和我们分享你希望用它达到的目标吗? – 2014-10-22 08:34:43