gpt4 book ai didi

c - 当我遇到1时使用递归进行分段

转载 作者:行者123 更新时间:2023-11-30 16:39:19 25 4
gpt4 key购买 nike

我应该使用递归来检查所有邻居,例如摩尔邻居,并检查随机生成的数组中是否有使用摩尔邻居连接的所有 1。我的递归代码在遇到零时似乎工作正常,但在遇到一时它会给我一个段错误。我尝试了 gdb 和 valgrind 但它给了我这个错误。

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a34 in recursive (arr=0x6030b0, xcoord=-1,
ycoord=0, row=6,
col=6) at as4.c:79
79 if(arr[xcoord][ycoord] == 1)

当我在 gdb()where 中输入 where 时:我得到了这个

#0  0x0000000000400a34 in recursive (arr=0x6030b0, xcoord=-1, 
ycoord=0, row=6,
col=6) at as4.c:79

#1 0x0000000000400a61 in recursive (arr=0x6030b0, xcoord=0,
ycoord=0, row=6,
col=6) at as4.c:83

#2 0x0000000000400975 in main (argc=3, argv=0x7fffffffdf48) at
as4.c:56

所以我不确定我是否分配了错误的东西,或者我的递归是否只是错误的。有人可以告诉我我做错了什么吗?

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

int recursive(int **arr, int xcoord, int ycoord, int row, int col);//What are the arguments for the recursive function?

int main(int argc, char** argv)
{
int i;//counters
int j;//counters
int xcoord;//x coordinate input
int ycoord;//y coordinate input

//random number generator thing idk lol
srand((unsigned int)time(NULL));

int row = strtol(argv[1], NULL, 0);//ROW from command line arguments (1st number)
int col = strtol(argv[2], NULL, 0);//COL from command line arguments (2nd number)

int *arrStorage = malloc(row * col * sizeof(int));//dynamically allocating memory for 2d array
int **arr = malloc(row * sizeof(int)); //pointer to pointer to array or whatever

//intializing array
for (i = 0; i < row; i++)
{
arr[i] = arrStorage + col * i;
}

//printing out 2d array
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = rand() % 2;
printf("%d\t", arr[i][j]);
}

printf("\n");
}

printf(" ");

//Exit the function when non number is entered
//Otherwise continue
while(1)
{
printf("Enter coordinate i,j (Non numeric to quit) \n");

if(1!=scanf("%d", &xcoord) || 1!=scanf("%d", &ycoord))
{
return 0;
}

printf("Blob size: %d\n", recursive(arr, xcoord, ycoord, row, col));
printf("The total size it takes up is %d percent \n", recursive(arr, xcoord, ycoord, row, col)/(row*col));
}

for (i = 0; i < row; i++)
{
free(arr[i]);
}
}

int recursive(int **arr, int xcoord, int ycoord, int row, int col)
{
int blobsize = 0;

//if coordinate is out of bounds or the user puts in too small or big coordinate return 0
if(xcoord < 0 && ycoord < 0 && xcoord > row && ycoord > col)
{
return 0;
}

//recursively check all the neighbors
else
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;

if(recursive(arr, xcoord - 1, ycoord, row, col))//Check up
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord - 1, ycoord + 1, row, col))//Check right up
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord, ycoord + 1, row, col))//Check right
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord + 1, ycoord + 1, row, col))//Check bottom right
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord + 1, ycoord, row, col))//Check bottom
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord + 1, ycoord-1, row, col))
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord, ycoord-1, row, col))
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}

if(recursive(arr, xcoord - 1, ycoord - 1, row, col))
{
if(arr[xcoord][ycoord] == 1)
{
blobsize = blobsize + 1;
}
return 1;
}
}
}

return blobsize;
}

是的,我确实必须使用递归来检查所有邻居,如果在某个位置遇到 1,则将 blobsize 加 1。

最佳答案

你的递归永远不会结束,程序会因为堆栈溢出而失败。例如,对于6x6,它会检查...(4,4),(5,5),(4,4),(5,5)...无休止地调用recursive,吹向上堆栈。

另外,释放两个指针,例如

free(arrStorage)
free(arr)

因为 libc/kernel 不知道如何free(a[i])

关于c - 当我遇到1时使用递归进行分段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47085715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com