Jump to content

Segmantation fault with pointers

MisterWhite
Go to solution Solved by MisterWhite,

Asked my professor today about this. It appears that here is an infinite recursion because I don't check if the we were here already. e.g. the cursor is in the right most position (next block is a wall) the recursive call goes back one step and the right again and so on.

I tried to make a maze solving function as part of the labyrinth game. I get segmentation fault on if(checkBoundries()) (code is below). is the mistake in the function call or there's some logic issue?

Spoiler

int checkBoundries(int currX,int currY,int rows,int columns, char **map)
{
    if( (currX>=0 && currX<=rows-1) && (currY>=0 && currY<=columns-1) )
    {

        if(map[currX][currY]!='#')
            return 1;
    }
    return 0;
}

int solveMaze(int rows, int columns,int currX, int currY, char ***map)// need fixing
{
    if(checkBoundries(currX,currY,rows,columns,*map))
    {
        if( (*map)[currX][currY]=='$')// base case
        {
            (*map)[currX][currY]='@';
            return 1;
        }
        (*map)[currX][currY]='.';

        //going right
        if(solveMaze(rows,columns,currX,currY+1,map)==1 )
            return 1;
        //left
        if(solveMaze(rows,columns,currX,currY-1,map)==1)
            return 1;
        //down
        if(solveMaze(rows,columns,currX+1,currY,map)==1)
            return 1;
        //up
        if(solveMaze(rows,columns,currX-1,currY,map)==1)
            return 1;

        //if there is no solution - undo
        (*map)[currX][currY]=' ';
        return 0;
    }
    return 0;
}

 

this is how i call solveMaze():

solveMaze(rows,columns,move.x,move.y,&map);// char **map; move.x/y current possition, rows/columns dimensions of maze

 

'.' - solution path marking

' ' - possible way

'@' - current possition

'#' - wall

'$'  - finish line.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Use a debugger like the ones included in Visual Studio or Xcode, GDB (if you compile with GCC), LLDB (if you compile with LLVM/Clang), Valgrind, etc.

Writing a C++ program without a debugger is almost impossible.

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, mathijs727 said:

Use a debugger like the ones included in Visual Studio or Xcode, GDB (if you compile with GCC), LLDB (if you compile with LLVM/Clang), Valgrind, etc.

Writing a C++ program without a debugger is almost impossible.

i used it.

first error is in

if(checkBoundries(currX,currY,rows,columns,*map))

second and third repeat like 5 times with the same values here

 if(solveMaze(rows,columns,currX,currY+1,map)==1 )
            return 1;
        //left
        if(solveMaze(rows,columns,currX,currY-1,map)==1)
            return 1;

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Spoiler

int getMapFromFile(char ***map)
{
  FILE *load=fopen("map.txt","r");
  *map=malloc(sizeof(char*)*20);//max 20 rows
  int rows=0;
 while(!foef(load))
 {
   (*map)[rows]=malloc(sizeof(char)*30);// max 30 columns
   fgets((*map)[rows],30,laod);
   rows++;
 }
  return rows;
}

int main()
{
	char **map;
  	int rows;
  
  rows=getMapFromFile(&map);
}

 

I really don't think that how i allocated memory for map is the issue because it works fine in other functions that are in the program.

Though is this function call correct

checkBoundries(currX,currY,rows,columns,*map)

?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

if( (currX>=0 && currX<=rows-1) && (currY>=0 && currY<=columns-1) )

You have the X position checking against rows and Y checking columns, is that intentional? Most people would do it the other way.

 

edit: nvm looks like you have it the same everywhere.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

int getMapFromFile(char ***map)

Being a 3 star programmer is not a compliment. You're doing something very wrong if you have to resort to this imho. It's a telltale sign your code needs to be simplified, which will probably solve the bug in the process.

 

I'd flatten the whole thing out into a normal 1D array and simply use index arithmetic to access rows/columns. It'll be less of a memory management hassle and passing the array along to functions will be much cleaner. The index arithmetic can be neatly wrapped in some accessor functions.

Link to comment
Share on other sites

Link to post
Share on other sites

What happens if you parse the map by reference and not by its pointer?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Nineshadow said:

What happens if you parse the map by reference and not by its pointer?

in which part? when getting the map from file?

sorry i'm a bit confused.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Asked my professor today about this. It appears that here is an infinite recursion because I don't check if the we were here already. e.g. the cursor is in the right most position (next block is a wall) the recursive call goes back one step and the right again and so on.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×