2011-12-15 103 views
0

我们目前正在研究一个需要处理一些文本的项目,为此,我们需要将文本分成较小的部分。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]); 
     } 
    } 

} 

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

更新1 我已经改变为使用3维的数组来存储文本段中的代码,但我仍然得到一个分段错误当我尝试使用malloc分配内存。

localText->list[i][n] = malloc(100 * sizeof(char)); 

她是已更改的代码。

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; 

} 
+2

您应该在调试器中运行您的程序。当它崩溃时,您将能够检查变量的值。 – 2011-12-15 11:05:06

+2

这是我第一次看到`for(n = 0; n 2011-12-15 11:08:10

回答

7

有很多代码中的错误的。下面是最严重的:

1)的指针到指针不是多维数组。如果您使用指针指针来访问多维动态分配的数组,那么该数组需要以对指针指针有意义的方式进行分配。

看起来好像你试图动态地分配一个指针数组,然后为该数组中的每个指针分配一个数据数组。但是,您的代码不会执行此操作,因此您的代码具有太多的间接性,无法让您的代码变得有意义。例如paragraph *list;,为什么你需要一个指向包含指向指针的结构的指针?

您需要简化您的数据结构。我建议做这样的代替:

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

2)不要命名的typedef同样的事情与结构标记,这将让你在命名空间冲突迟早出事。当typedef:结构体时,甚至不需要结构体标签,而是按照上面的示例进行操作。

3)不要使用C语言对malloc/calloc的结果进行类型转换。这隐藏了编译器警告和错误。无数详细的帖子可以在这里找到原因,在SO

4)由于这是一个在OS上运行的托管程序(通过使用文件处理我可以知道),main不能返回除int之外的任何内容。将主体的定义更改为int main()它不会在标准C编译器上编译。

5)for(n = 0; n < i ; n++) ... list[i]。正如你可以通过你自己的代码所说的,对于除了循环迭代器之外的任何东西,使用变量名称i都不是一个好主意。 (i实际上代表迭代器)。这就是为什么你有一个错误。

6)您必须在完成后通过fclose()关闭打开的文件。

7)当您完成动态分配的内存时,您必须通过free()取消分配动态分配的内存。

5
readFileContent(file, &localText->list[i], 50); 

您在这里初始化一个过去最最后一个个元素,而不是初始化所有其他列表元素。改为尝试list[n]

0

看来你已经做在这里

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", n); 
     readFileContent(file, &localText->list[n], 50); 

    }