gpt4 book ai didi

C段错误

转载 作者:行者123 更新时间:2023-11-30 18:47:30 25 4
gpt4 key购买 nike

我是编码新手,所以我知道我可能会搞砸一些事情。我想创建一个 (number_of_text_words)x(number_of_letters_of every_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 编译显示了以下内容:

* Error in `./crossword': free(): invalid next size (fast): 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] ======= Memory map: ======== 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 [heap] 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] Aborted (core dumped)

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

最佳答案

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

dict=malloc(sum);

应该是

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

因为你需要sum大小为void *的元素,但最好写成sizeof(*dict),因为这样代码与 dict 的类型无关,如果您更改它,则无需修复 malloc()

这看起来也很可疑,

num_words=malloc(sum);

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

关于C段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com