简体   繁体   English

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

[英]Segmentation fault on trying to pass a recursive function to populate a 2D array

I'm trying to pass a recursive function that populates my 2D array of structs. 我正在尝试传递一个递归函数来填充我的2D结构数组。 My memory allocation is working fine, but when I try to do a recursion, I get the error: Segmentation fault (core dumped). 我的内存分配工作正常,但是当我尝试进行递归时,我收到错误:Segmentation fault(core dumped)。 Any idea why this must be happening? 知道为什么会这样吗? I think I wrote my code so that no index out of bound occurs. 我想我编写了我的代码,以便不会出现索引超出范围。 I still don't know why this is happening. 我仍然不知道为什么会这样。 Any help is going to be appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

#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
    }   
}

each of the if() code blocks in the last half of assign() are beginning with the same basic parameter values, except the limit changes. assign()的后半部分中的每个if()代码块都以相同的基本参数值开头,但限制更改除外。

so the total number of recursions (which the code seems to be limiting the the value in 'limit' is not actually limited, 所以递归的总数(代码似乎限制了'限制'中的值实际上并没有限制,

because when limit is 0, it is not necessarily the last call to be made to assign() and once limit is <0 the code will recurse about 16 gig more times. 因为当limit为0时,它不一定是对assign()的最后一次调用,一旦limit <0,代码将再次递归约16 gig。 (at least) This is probably why the program crashes (至少)这可能是程序崩溃的原因

Suggest decrementing limit within assign() before any of the recursive calls to assign() 推荐内递减限制assign()之前的任何递归调用的assign()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM