-
November 27th, 2015, 08:28 AM
#1
Strange flow of control in a c program
I have been assigned to code a 'maze' program which will allow user to play and solve a random maze by navigating from start to end. So far I've developed a working prototype that runs well for predefined mazes, but the institution requires me to make it as random as possible. For that I included a function 'findpath' that check and verifies if the random maze can be solved or otherwise call 'mazebuilder' to create another random maze. The whole code is here : http://codepad.org/wb1OGGrZ .
Now while execution, this function shows illegal flow of control.
int findpath(int x, int y)
{
if(fpmap[x][y]==END) //1
{
return TRUE;
}
if(fpmap[x][y]!=PATH||fpmap[x][y]!=START) //2
{
return FALSE; //2a
}
min_moves++; //3
fpmoves++;
fpmap[x][y]=SOLUTION;
if(findpath(x,y-1)) //4
{
return TRUE;
}
if(findpath(x+1,y))
{
return TRUE;
}
if(findpath(x,y+1))
{
return TRUE;
}
if(findpath(x-1,y))
{
return TRUE;
}
min_moves--; //5
fpmap[x][y]=PATH;
return FALSE; //6
}
I tried to trace the program and this is what the function does upon its call :
1. Checks if #1.
2. Checks if #2.
3. Skips to #6.
So, why didn't the program go to #2a or #3 if's after #4 or #5?
It just seems to skip whole code and rush to #6. Is there a logical error in this or is this syntactical?
Please help me out of this.
PS: This code has been written for TurboC compiler because my faculties require me to do so. Please bear with me
More info on the algorithm used in findpath : http://www.cs.bu.edu/teaching/alg/maze
-
November 29th, 2015, 03:39 PM
#2
Re: Strange flow of control in a c program
You'll need to post properly formatted code to get help here. Regardless, this is exactly the kind of problem your debugger will help you solve easily.
-
December 3rd, 2015, 07:17 PM
#3
Re: Strange flow of control in a c program
My guess is that the condition on 2 evaluates to true, and that the optimized code jumps to 6 instead of generating another return.
You can confirm this by adding a trace before the
return FALSE // 2a
and see that the trace is being executed.
Last edited by RichardDuNord; December 4th, 2015 at 12:11 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|