Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
C++
#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; 
}
Posted
Updated 11-May-14 17:26pm
v2
Comments
[no name] 11-May-14 20:06pm    
http://en.wikipedia.org/wiki/Segmentation_fault
CPallini 12-May-14 3:40am    
I ran your code and got NO segmentation fault, rather is keep printing a non sense (to me) sequence of numbers.<br>
Could you please provide more info?
Legor 12-May-14 4:21am    
I compiled and ran you code and also got no segmentation fault.
KarstenK 12-May-14 6:56am    
more details: which OS and which compiler?

How did you produce the error? Input?

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.
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900