C代码中的分段错误 [英] Segmentation fault in C code

查看:75
本文介绍了C代码中的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我正在创建一种常见的填充区域,但有数字。但是我遇到了分段错误的问题。你能告诉我为什么吗?这是我的代码:

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

/ * ???????? ?? ??? T 530 S,???? S' 12 O 19 * /
void help( int a [ 20 ] [ 60 ], int b [ 20 ] [ 60 ], int i, int j)
{
if (i-1< 20&& i-1> ; = 0 &&(i-1!= 0 || j!= 0 )&& a [i- 1 ] [j] == a [ 0 ] [ 0 ])
{
b [i- 1 ] [j] = 1 ;
help(a,b,i- 1 ,j);
}
if (j-1< 60&& j-1> = 0 &&(i!= 0 || j-1!= 0 ) && a [i] [j] == a [ 0 ] [ 0 ]){
b [i] [j- 1 ] = 1 ;
help(a,b,i,j- 1 );
}
if (i + 1< 20& i + 1> = 0 && a [i + 1] [j] == a [ 0 ] [ 0 ]){
b [i + 1] [j] = 1 ;
help(a,b,i + 1,j);
}
if (j + 1< 60& j + 1> = 0 && a [i] [j + 1] == a [ 0 ] [ 0 ]){
b [i] [j + 1] = 1 ;
help(a,b,i,j + 1);
}
}

void 游戏( int a [ 20 ] [ 60 ], int b [ 20 ] [ 60 ], int i, int j)
{
int c;
scanf( %d,& c);
for (i = 0 ; i< 20; i ++)
的类=code-keyword>(j = 0 ; j< 60; j ++)
if (b [i] [j] == b [ 0 ] [ 0 ])
a [i] [j] = c;
}
int end( int a [ 20 ] [ 60 ])
{
int 我,j;
for (i = 0 ; i< 20; i ++)
的类=code-keyword>(j = 0 ; j< 60; j ++)
if (a [i] [j]!= a [ 0 ] [ 0 ]) return 1 ;
return 0 ;
}
int main( int argc, char * argv []){
int a [ 20 ] [ 60 ],b [ 20 ] [ 60 < /跨度>],I,J,K;
srand(time(NULL));
k = 0 ;
for (i = 0 ; i< 20; i ++)
的类=code-keyword>(j = 0 ; j <60; j ++)
{
b [i] [j] = 0 ;
a [i] [j] = rand()% 5 + 1 ;
}
b [ 0 ] [ 0 ] = 1 ;
for (i = 0 ; i< 20; i ++)
的类=code-keyword>(j = 0 ; j <60; j ++)
{
if (j == 59 )printf( < span class =code-string>%d \ n
,a [i] [j]);
else printf( %d< /跨度>中,[i] [j]);

}
执行
{
help(a,b, 0 0 );
游戏(a,b, 0 0 );
for (i = 0 ; i< 20; i ++)
的类=code-keyword>(j = 0 ; j <60; j ++)
{
if (j == 59 )printf( < span class =code-string>%d \ n
,a [i] [j]);
else printf( %d< /跨度>中,[i] [j]);
}
for (i = 0 ; i< 20; i ++)
for (j = 0 ; j< 60; j ++)
{
if (j == 59 )printf( %d \ n,b [i] [j]);
else printf( %d< /跨度>,b [i] [j]);
}
}
while (end(a)== 1 );
return 0 ;
}

解决方案

1。寻找错误。

不要使用随机数字,错误可能无法重现。

或者他们有时会在那里,有时候不会。



2.我不知道你的程序是做什么的,但我让它崩溃了。

使用vs2012。 srand(0);并输入5

我得到一个堆栈溢出调用void help(int a [20] [60],int b [20] [60],int i,int j);

深度可能超过100次。

或者,更改编译器关于堆栈空间的设置,

或者最好,尝试重写递归作为for循环。


可能的访问来源超出范围(可能导致分段错误):

- 帮助,首先如果: j 未进行边界检查以进行读写

- 帮助,第二条如果: i 不是边界检查读写

- 帮助,第三个如果: j 未进行边界检查以进行读写

- 帮助,第四个if: i 没有边界检查读写

我假设如果 s应为 if ... else if else如果... else if else ... 而不是 if ... if ... if ... if ... 依次。



更多,您很难跟踪帮助功能中的递归,主要是由于缺少 else s。



修复那个否则如果...... 再试一次。



干杯

Andi

Hi guys. I'm creating a game which is common known as fill-zone but with numbers. However I'm having issues with segmentation fault. Can you tell me why? Here is my code:

#include <stdio.h> 
#include <stdlib.h> 
  
/* ???????? ?? ???T???S,????S? ??O?? */
void help(int a[20][60],int b[20][60],int i, int j)
{
 if(i-1<20 && i-1>=0 && (i-1!=0 || j!=0) && a[i-1][j]==a[0][0])
 {
  b[i-1][j]=1;
  help(a,b,i-1,j);
 }
 if(j-1<60 && j-1>=0 && (i!=0 || j-1!=0) && a[i][j]==a[0][0]){
  b[i][j-1]=1;
  help(a,b,i,j-1);
 }
 if(i+1<20 && i+1>=0 && a[i+1][j]==a[0][0]){
  b[i+1][j]=1;
  help(a,b,i+1,j);
 }
 if(j+1<60 && j+1>=0 && a[i][j+1]==a[0][0]){
  b[i][j+1]=1;
  help(a,b,i,j+1);
 }
}

void game(int a[20][60],int b[20][60],int i,int j)
{
	int c;
	scanf("%d",&c);
	for (i=0;i<20;i++)
		for (j=0;j<60;j++)
			if (b[i][j]==b[0][0])
				a[i][j]=c;
}
int end(int a[20][60])
{
	int i,j;
	for (i=0;i<20;i++)
		for (j=0;j<60;j++)
			if (a[i][j]!=a[0][0]) return 1;
	return 0;					
}
int main(int argc, char *argv[]) { 
    int a[20][60],b[20][60],i,j,k;
    srand (time(NULL));
	k=0; 
    for (i=0;i<20;i++) 
        for (j=0; j<60; j++) 
        { 
        	b[i][j]=0;
            a[i][j]= rand() % 5 + 1 ; 
        } 
    b[0][0]=1;
    for (i=0;i<20;i++) 
        for (j=0; j<60; j++) 
        { 
            if (j==59) printf("%d\n",a[i][j]); 
            else printf("%d",a[i][j]);   
              
        } 
    do
    {
    	help(a,b,0,0);
    	game(a,b,0,0); 	
		for (i=0;i<20;i++) 
	        for (j=0; j<60; j++) 
	        { 
	            if (j==59) printf("%d\n",a[i][j]); 
	            else printf("%d",a[i][j]);   
			}
		for (i=0;i<20;i++) 
	        for (j=0; j<60; j++) 
	        { 
	            if (j==59) printf("%d\n",b[i][j]); 
	            else printf("%d",b[i][j]);   
			}	
	}
    while (end(a)==1);  
    return 0; 
}

解决方案

1. When looking for bugs.
Don't use random numbers, errors might not be reproducible.
Or they might sometimes be there and sometimes not.

2. I don't know what your program does, but I got it to crash.
Using vs2012. "srand (0);" and with input "5"
I get a stack overflow calling void help(int a[20][60],int b[20][60],int i, int j);
It is probably more than 100 calls deep.
Either, change the setting for the compiler regarding stack space,
or preferably, Try to rewrite the recursion as a for loop.


Possible sources of access out of bound (which may lead to a segmentation fault):
- help, first if: j is not boundary checked for read and write
- help, second if: i is not boundary checked for read and write
- help, third if: j is not boundary checked for read and write
- help, fourth if: i is not boundary checked for read and write
I assume the ifs in the help function should be if ... else if ... else if ... else if ... and not if ... if ... if ... if ... in sequence.

Further more, you have difficult to track recursions in the help function, mainly due to the missing elses.

Fix that else if ... and try again.

Cheers
Andi


这篇关于C代码中的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆