CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2015
    Posts
    1

    Unhappy 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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Dec 2015
    Posts
    2

    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
  •  





Click Here to Expand Forum to Full Width

Featured