C 尝试传递递归函数以填充2D数组时出现分段错误

C 尝试传递递归函数以填充2D数组时出现分段错误,c,arrays,recursion,C,Arrays,Recursion,我试图传递一个递归函数,该函数填充我的2D结构数组。我的内存分配工作正常,但当我尝试执行递归时,我得到了一个错误:分段错误(内核转储)。 知道为什么会这样吗?我想我编写代码是为了避免索引越界。我仍然不知道为什么会这样。任何帮助都将受到感激。谢谢 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> typedef struct { char val; bool filled;

我试图传递一个递归函数,该函数填充我的2D结构数组。我的内存分配工作正常,但当我尝试执行递归时,我得到了一个错误:分段错误(内核转储)。 知道为什么会这样吗?我想我编写代码是为了避免索引越界。我仍然不知道为什么会这样。任何帮助都将受到感激。谢谢

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

typedef struct {
    char val;
    bool filled;
} elements;

void assign(elements ** elements, int row, int column, int x, int y, int limit);

int main(int argc, char** argv)
{
    int row = 0;
    int column = 0;
    int x = 0;
    int y = 0; 
    int limit = 0;

    sscanf(argv[1], "%d", &row);
    sscanf(argv[2], "%d", &column);
    sscanf(argv[3], "%d", &x);
    sscanf(argv[4], "%d", &y);
    sscanf(argv[5], "%d", &limit);

    elements **foo;

    foo = (elements **)malloc(sizeof(elements *) * row);
    for (int i = 0; i < column; i++)
        foo[i] = (elements *)malloc( sizeof(elements) * row);

    foo[y][x].val = 'C';
//  printf("%c\n", foo[y][x].val);
    assign(foo, row, column, x, y, limit);

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < column; j++)
        {

        //  foo[i][j].val = '.';
            printf("%d\t ", foo[i][j].filled);
        }
        printf("\n");
    }


}

void assign(elements ** elements, int row, int column, int x, int y, int limit)
{
    int tempX = x;
    int tempY = y;
    if(elements[y][x].filled != 0 )
    {
        //printf("reached.");
        return;
    }
    else if(limit < 0)
    {
        //printf("reached.");
        return;
    }
    else
    {
        if(elements[y][x].val != 'C')
            elements[y][x].val = limit + '0';
        elements[y][x].filled = true;

        tempX = x - 1;
        tempY = y;
        if (!( x < 0 || y < 0 || x > column - 1 || y > row -1 ))    
            assign(elements, row, column, tempX, tempY, limit - 1); // go up
        tempX = x;
        tempY = y + 1;
        if (!( x < 0 || y < 0 || x > column - 1 || y > row -1 ))    
            assign(elements, row, column, tempX, tempY, limit - 1); // go right
        tempX = x + 1;
        tempY = y;
        if (!( x < 0 || y < 0 || x > column - 1 || y > row -1 ))    
            assign(elements, row, column, tempX, tempY, limit - 1); // go down
        tempX = x;
        tempY = y - 1;
        if (!( x < 0 || y < 0 || x > column - 1 || y > row -1 ))    
            assign(elements, row, column, tempX, tempY, limit - 1); // go left
    }   
}
#包括
#包括
#包括
类型定义结构{
char-val;
布尔填充;
}元素;
无效分配(元素**元素,整数行,整数列,整数x,整数y,整数限制);
int main(int argc,字符**argv)
{
int行=0;
int列=0;
int x=0;
int y=0;
整数极限=0;
sscanf(argv[1]、“%d”和行);
sscanf(argv[2]、“%d”和列);
sscanf(argv[3],%d“,&x);
sscanf(argv[4],%d“,&y);
sscanf(argv[5]、“%d”、&limit);
元素**foo;
foo=(元素**)malloc(sizeof(元素*)*行);
对于(int i=0;i列-1 | | y>行-1))
赋值(元素、行、列、tempX、tempY、limit-1);//向上
tempX=x;
tempY=y+1;
如果(!(x<0 | | y<0 | | x>列-1 | | y>行-1))
赋值(元素、行、列、tempX、tempY、limit-1);//向右
tempX=x+1;
tempY=y;
如果(!(x<0 | | y<0 | | x>列-1 | | y>行-1))
分配(元素、行、列、tempX、tempY、限制-1);//向下
tempX=x;
tempY=y-1;
如果(!(x<0 | | y<0 | | x>列-1 | | y>行-1))
赋值(元素、行、列、tempX、tempY、limit-1);//向左
}   
}
assign()后半部分中的每个if()代码块都以相同的基本参数值开头,但限制更改除外

因此递归的总数(代码似乎限制了“limit”中的值)实际上并没有限制

因为当limit为0时,它不一定是对assign()的最后一次调用,并且一旦limit为,assign()的后半部分中的每个if()代码块都以相同的基本参数值开始,除了limit更改之外

因此递归的总数(代码似乎限制了“limit”中的值)实际上并没有限制


因为当limit为0时,它不一定是对assign()的最后一次调用,并且一旦limit为
foo[i]=malloc(sizeof(elements)*行);
-->-->
foo[i]=malloc(sizeof(elements)*列);
旁注:您没有分配2D数组,;)分配
个数的
元素*
,然后循环
元素来分配
个数的
元素
结构。如果不提供正确的参数数量,您将有未定义的行为,超出
argv
的范围。您应该始终检查
argc
以确保参数的数量正确。
if(elements[y][x].filled!=0)
:请注意,
malloc
不会初始化成员…正如…
foo[i]=malloc(sizeof(elements)*行);
-->
foo[i]=malloc(sizeof(elements)*列)
旁注:您没有分配2D数组,;)您分配了
元素数*,然后循环
元素以分配
元素数结构。如果不提供正确的参数数,则会超出
argv
的范围,导致行为未定义。您应该始终检查
argc
以确保参数的数量正确。
if(elements[y][x].filled!=0)
:注意
malloc
不会初始化成员。。。。就像。。。