我是编码的新手,所以我知道我可能搞砸了一些事情。我想创建一个(number_of_text_words)x(number_of_letters_of each_word)维度的数组,在那里我将保存单词,逐个字符并放置' \ 0'最后,以便被识别为一个字符串。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char **argv)
{
FILE *myinput;
int i,k,sum,words_num,j;
char **dict=NULL;
char *num_words=NULL;
myinput = fopen("bug.txt","r");
if (myinput == NULL)
{
fprintf(stderr, "ERROR:\n");
return;
}
fseek(myinput,0,SEEK_END);
int size = ftell(myinput);
fseek(myinput,0,SEEK_SET);
char *data = malloc(size);
if (data==NULL)
return -1;
fread(data,size,1,myinput);
fclose(myinput);
sum=0;
for (i=0;i<=size-1;i++)
if (data[i]=='\n')
sum++;//count the words by the spaces
/*for (i=0;i<=size-1;i++)
printf("%c\n",data[i]);*/
/*printf("\n%d\n",sum);*/
dict=malloc(sum);//for saving the words
if (dict==NULL)
return -1;
num_words=malloc(sum);//for saving the number of letters of each word
if (num_words==NULL)
return -1;
j=0;
for (i=0;i<sum;i++){
words_num=0;
while ((data[j++]!='\n'))
words_num++;//allocate the exact needed memory for each word
dict[i]=malloc(words_num+1);//FOR THE FINAL '\0'
if (dict[i]==NULL)
return -1;
num_words[i]=words_num;
}
k=0;
for (i=0;i<sum;i++){
for(j=0;j<num_words[i];j++){
if ((data[k]=='\n') || (data[k]==EOF))
k++;
if (k<size)
dict[i][j]=data[k++];
}
if (k<=size)
dict[i][j+1]='\0';
}
for (i=0;i<sum;i++)
free(dict[i]);
free(dict);
free(num_words);
free(data);
return 0;
}
它无法编译,因为它显示了一个分段错误。更具体地说,-g编译向我展示了以下内容:
*`./crossword' ;: free()出错:下一个大小无效(快):0x0000000000fdf010 * ======= Backtrace:========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f7b54dc87e5] /lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f7b54dd137a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f7b54dd553c] ./crossword[0x400a97] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f7b54d71830] ./crossword[0x400699] =======记忆图:======== 00400000-00401000 r-xp 00000000 08:25 532064
/ home / nikos / Desktop / ergasies / ergasia_4 / crossword 00600000-00601000 r - p 00000000 08:25 532064
/ home / nikos / Desktop / ergasies / ergasia_4 / crossword 00601000-00602000 rw-p 00001000 08:25 532064
/ home / nikos / Desktop / ergasies / ergasia_4 / crossword 00fdf000-01000000 rw-p 00000000 00:00 0 [堆] 7f7b50000000-7f7b50021000 rw-p 00000000 00:00 0 7f7b50021000-7f7b54000000 --- p 00000000 00:00 0 7f7b54b3b000-7f7b54b51000 r-xp 00000000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.so.1 7f7b54b51000-7f7b54d50000 --- p 00016000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.so.1 7f7b54d50000-7f7b54d51000 rw-p 00015000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.so.1 7f7b54d51000-7f7b54f11000 r-xp 00000000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b54f11000-7f7b55111000 --- p 001c0000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55111000-7f7b55115000 r - p 001c0000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55115000-7f7b55117000 rw-p 001c4000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55117000-7f7b5511b000 rw-p 00000000 00:00 0 7f7b5511b000-7f7b55141000 r-xp 00000000 08:25 542656 /lib/x86_64-linux-gnu/ld-2.23.so 7f7b55323000-7f7b55326000 rw-p 00000000 00:00 0 7f7b5533f000-7f7b55340000 rw-p 00000000 00:00 0 7f7b55340000-7f7b55341000 r - p 00025000 08:25 542656
/lib/x86_64-linux-gnu/ld-2.23.so 7f7b55341000-7f7b55342000 rw-p 00026000 08:25 542656
/lib/x86_64-linux-gnu/ld-2.23.so 7f7b55342000-7f7b55343000 rw-p 00000000 00:00 0 7fff2dca9000-7fff2dcca000 rw-p 00000000 00:00 0
[stack] 7fff2dd02000-7fff2dd05000 r - p 00000000 00:00 0
[vvar] 7fff2dd05000-7fff2dd07000 r-xp 00000000 00:00 0
[vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]中止(核心倾销)
我还创建了一个用于保存每个单词大小的数组。我没有双重释放任何指针,也没有找到任何&#34;阵外的&#39;写作。任何人都可以指出我的代码中出错的东西吗?真的很感激它:)
答案 0 :(得分:2)
你没有分配足够的空间,这里
dict=malloc(sum);
应该是
dict = malloc(sum * sizeof(*dict));
因为您需要sum
个void *
大小的元素,但编写sizeof(*dict)
会更好,因为这样代码与{{1}的类型无关如果你改变它,你就不需要修改你的dict
。
这看起来也很可疑,
malloc()
你怎么知道每个单词的字符数与文件中的行数一样多?