C 2D 字符串数组在其声明的函数之外导致段错误

标签 c string memory-management segmentation-fault dynamic-memory-allocation

相关代码旨在从 txt 文件加载数据,稍后将使用该数据来玩《Conways Game of Life》的控制台版本。数据类型是一个二维字符串数组,以便可以存储生命游戏的进一步迭代,以便检查振荡模式。它将数组通过引用传递给“readworld”函数。它从文本文件的顶部加载 future 数组的迭代次数、宽度和高度。

此代码的问题在于它从文本文件加载并将其成功保存在“loadWorld”函数中。这可以通过在函数末尾打印输出来证明。但是,当在“main”函数中访问同一数组时,第一个元素会发生段错误。

这很奇怪,因为由 malloc 分配的内存应该在堆上分配,因此可用于其他函数,除非我遗漏了一些东西......

我不确定是否应该发布文本文件,但如果应该,请发表评论,它将被发布。

任何帮助将不胜感激!

注释 使用 MinGW 4.7.2 编译的文件。 文本文件的第一行包含 GOL 执行的列数、行数和迭代次数。 文本文件中的 x 代表活细胞,而空格代表死细胞。

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

void readworld(char ***,int *,int *,int*);

int main(){
    int rows,columns,ticks,repetition,count,count2;
    char ***world;
    readworld(world,&rows,&columns,&ticks);
    printf("Rows: %i Columns: %i Ticks: %i\n",rows,columns,ticks);
    for(count = 1; count < rows-1; count++){
        //Segmentation fault occurs here.                       
        printf("%s",world[0][count]);
    }

    system("PAUSE"); 
    return 0;

}

void readworld(char ***world,int *rows,int *columns,int *ticks){
    FILE *f;
    int x,y;


    //Load the file
    f=fopen("world.txt","r");
    //Load data from the top of the file. 
    //The top of the file contains number of rows, number of columbs and number of iterations to run the GOL
    fscanf(f,"%i %i %i\n", rows, columns, ticks);

    printf("%d %d %d\n",*rows, *columns, *ticks);

    *columns=*columns+2;  //Includes new line and end of line characters
    world=(char***) malloc(*ticks*sizeof(char**));      //makes an array of all the grids

    for (y=0;y<*ticks;y++){

        world[y]=(char**) malloc(*rows * sizeof(char*));    //makes an array of all the rows

        for (x=0;x<*rows;x++){
            world[y][x]=(char*) malloc( *columns * sizeof(char));   //makes an array of all the collumns
        }
    }

    for (y=0;y<*rows;y++){
        fgets(world[0][y],*columns,f);  //fills the array with data from the file
    }
    //Correctly prints the output from the textfile here
    for (y = 0 ; y < *rows; y++) 
            printf("%s",world[0][y]);
}

最佳答案

当您将对象传递给函数时,计算机会创建该对象的副本并为其分配相同的值。函数对复制对象所做的任何更改都不会应用于原始对象。

因此,在您的测试用例中(应该如下所示)

#include <stdlib.h>

void readworld(char ***);

int main(){
    char ***world;
    readworld(world);
}

void readworld(char ***world){
    world = malloc(42);
}

world main内不受对(不同)world 所做更改的影响readworld内。您想要将指针传递给 worldreadworld并修改指向的对象,类似于 rows , columnsticks如果您希望更改在 readworld 之外可见.

int main(void) {
    ...
    readworld(&world,&rows,&columns,&ticks);
    ...
}

void readworld(char ****world, int *rows, int *columns, int *ticks){
    ...
    *world = malloc(42);
    ...
}

关于C 2D 字符串数组在其声明的函数之外导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16860147/

相关文章:

java - 在其他字符串中查找字符串的任何符号的第一次出现

c++ - C++段错误问题

c - mikroc for pic 中的任务出错

c++ - 如何让 const char* 函数工作?

c++ - 接受字符串来调用函数

javascript - 在接受用户的答案之前,如何将我的问题显示到控制台?

C:线程之间的Fifo,写入和读取字符串

c - C 中的交换指针 - 不兼容警告

c - 内核模式访问用户空间变量

c# - 限制 .NET 进程可以使用的最大 RAM 量?