C—分段故障



我是编码新手,所以我知道我可能会把一些事情搞砸。我想创建一个由(number_of_text_words)x(number_of _tletters_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("%cn",data[i]);*/
/*printf("n%dn",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 ''
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]='';
}

for (i=0;i<sum;i++)
free(dict[i]);
free(dict);
free(num_words);
free(data);
return 0;
}

但它无法编译,因为它向我显示了分段错误。更具体地说,-g编译向我展示了以下内容:

*中出错/填字游戏':free():无效的下一个大小(快速):0x0000000000fdf010*=========回溯:==========/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]./填字游戏[0x400a97]/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f7b54d71830]./填字游戏[0x400699]======内存映射:==========00400000-00401000 r-xp 00000000 08:25 532064
/home/nikos/Desktop/ergasies/ergasia_4/填字游戏00600000-00601000r--p 00000000 08:25 532064
/home/nikos/Desktop/ergasies/ergasia_4/填字游戏00601000-00602000rw-p 00001000 08:25 532064
/home/nikos/Desktop/ergasies/ergasia_4/填字游戏00fdf000-01000000rw-p 00000000 00:00 0[堆]7f7b5000000-7f7b50021000 rw-p 00000000 00:00 07f7b50021000-7f7b54000000--0000000000:007f7b54b3b000-7f7b54-b51000 r-xp 00000000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.so.so.1 7f7b54/b51000-7f7b54 d50000--p00016000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.s.so.1 7f7b54d50000-7f7b54 d51000 rw-p00015000 08:25 529319
/lib/x86_64-linux-gnu/libgcc_s.s.so.1 7f7b54d51000-7f7b54 f11000 r-xp0000000008:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b54f11000-7f7b55111000--p001c0000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55111000-7f7b55315000 r--p001c0000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55115000-7f7b55117000 rw-p001c4000 08:25 542663
/lib/x86_64-linux-gnu/libc-2.23.so 7f7b55117000-7f7b5511b000 rw-p00000000 00:00 0 7f7b5511b0000-7f7b 55141000 r-xp 00000000 08:25 542656/lib/x86_64-linux-gnu/ld-2.223.so 7f7b55323000-7b55326000 rw-p00000000 00:00 0 7f7b5533f000-7f7b55540000 rw-p 00000000 00:0007f7b553400000-7f7b55341000 r--p 00025000 08:25 542656
/lib/x86_64-linux-gnu/ld-2.223.so 7f7b55141000-7f7b55542000 rw-p00026000 08:25 542656
/lib/x86_64-linux-gnu/ld-2.223.so 7f7b55342000-7f7b55143000 rw-p0000000000:00 0 7fff2dca9000-7fff2dcca000 rw-p 0000000000:00 0
[stack]7fff2dd02000-7fff2dd05000 r--p 0000000000:00 0\br>[vvar]7fff2dd05000-7fff2dd07000 r-xp 00000000 00:00 0
[vdso]ffffffffff 600000-fffffff2ffff 601000 r-xp 0000000000:00 0%
[vsyscall]中止(转储核心)

我还创建了一个数组来保存每个单词的大小。我没有双重释放任何指针,也找不到任何"数组外"的写入。有人能指出我代码中出现的问题吗?我真的很感激:)

您没有分配足够的空间,这里是

dict=malloc(sum);

应该是

dict = malloc(sum * sizeof(*dict));

因为您需要void *大小的sum元素,但最好编写sizeof(*dict),因为这样代码就独立于dict的类型,如果您更改了它,就不需要修复malloc()

这看起来也很可疑,

num_words=malloc(sum);

你怎么知道每个单词的字符数和文件中的行数一样多?

相关内容

  • 没有找到相关文章

最新更新