malloc问题导致分段错误

Mor*_*gøe 0 c malloc

我们目前正在开发一个需要处理某些文本的项目,为此,我们需要将文本分成较小的部分.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct paragraph{
   char **words;
}paragraph;

typedef struct text{
   char name[100];
   paragraph  *list;
}text;

void readFileContent(FILE *file, paragraph *pa, int size){

   char localString[100];

   pa->words = (char **)malloc(size * sizeof(char *));
   int i = 0, z;

   while(fscanf(file, "%s", localString) == 1 && i < size){
      z = strlen(localString);
      pa->words[i] = (char *)malloc(z + 1);

      strcpy(pa->words[i], localString);
      i++;
   }

}

void main(){
      int i = 0, n, z;
   FILE *file;
   text *localText;
   localText = (text *)malloc(sizeof(text));

   openFile(&file, "test.txt");
   i = countWords(file);

   i = i / 50 + 1; // calculate the number of section need for the text

   localText->list = calloc(sizeof(paragraph *), i);

   for(n = 0; n < i ; n++){
      printf("Paragraph - %d\n", n);
      readFileContent(file, &localText->list[i], 50);

   }

   for(n = 0; n < i ; n++){
      printf("Paragraph - %d", n);
      for(z = 0; z < 50; z++){
      printf("no. %d\n", z);
      printf("%s\n", localText->list[n].words[z]);
      }
   }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行程序时,我在底部的打印循环上出现了分段错误.我认为它是由分配内存的一些问题引起的,但我无法弄清楚原因.

更新1 我已经更改了代码以使用3维数组来存储文本段,但是当我尝试使用malloc分配内存时,我仍然遇到分段错误.

localText->list[i][n] = malloc(100 * sizeof(char));
Run Code Online (Sandbox Code Playgroud)

她是改变了的代码.

typedef struct {
   char name[100];
   char  ***list;
}text;

int main(){
   int i = 0, n, z,wordCount, sections;
   FILE *file;
   text *localText;

   openFile(&file, "test.txt");
   wordCount = countWords(file);


   sections = (wordCount / 50) + 1;

   localText = malloc(sizeof(text));
   localText->list = malloc(sections * sizeof(char **));

   for(i = 0; i < sections; i++)
      localText->list[i] = malloc(50 * sizeof(char *));
      for(n = 0; n < 50; n++)
         localText->list[i][n] = malloc(100 * sizeof(char));

   readFileContent(file, localText->list, 50);

   freeText(localText);

   return 1;

}
Run Code Online (Sandbox Code Playgroud)

Lun*_*din 7

你的代码中有很多bug.以下是最严重的问题:

1)指向指针的指针不是多维数组.如果使用指向指针来访问多维,动态分配的数组,则需要以对指针指针有意义的方式分配该数组.

看起来你正在尝试动态分配一个指针数组,然后对于该数组中的每个指针,分配一个数据数组.但是,您的代码不会执行此操作,您的代码间隔太多,无论如何都没有任何意义.例如paragraph *list;,为什么需要一个指向包含指针指针的结构的指针?

您需要简化数据结构.我建议这样做:

typedef struct {
   char   name[100];
   char** list;
} text;
Run Code Online (Sandbox Code Playgroud)

2)不要将typedef命名为与struct标签相同的东西,这会让你迟早遇到命名空间冲突问题.在typedef:ing a struct时你甚至不需要struct标签,而是像我上面的例子那样.

3)永远不要在C语言中对malloc/calloc的结果进行类型转换.这隐藏了编译器警告和错误.在SO上可以找到无数关于原因的详细帖子.

4)因为这是在OS上运行的托管程序(我可以通过使用文件处理来判断),main除了int之外不能返回任何东西.将main的定义更改为int main()或者不会在标准C编译器上编译.

5)for(n = 0; n < i ; n++) ... list[i].正如您自己的代码所说i,除了循环迭代器之外,使用变量名称不是一个好主意.(i实际上代表迭代器).这就是你在那里遇到一个bug的原因.

6)完成后,您必须关闭打开的文件fclose().

7)完成后,必须释放动态分配的内存free().


thi*_*ton 5

 readFileContent(file, &localText->list[i], 50);
Run Code Online (Sandbox Code Playgroud)

您正在初始化此处的最后一个元素,而不是初始化所有其他列表元素.试试吧list[n].