Bad Memory Allocation

I have a 2D dynamic Array, I was able to fill it up properly but every time I run this function it gives me a bad memory allocation error and terminates the program. Any ideas why this function messes it up?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int Maze::SolveMaze(char **a, int x, int y)
{
     
     if (a[y][x] != '.')
        return false;
     //Marks as part of solution   
     a[y][x]='!';
     
     if(SolveMaze( a,x,y-1)==true) return true;//North
     if(SolveMaze( a,x+1,y)==true) return true;//East
     if(SolveMaze( a,x,y+1)==true) return true;//South
     if(SolveMaze( a,x-1,y)==true) return true;//West
     //Marks as not part of the solution
     a[y][x]='x';
     
     return false;
        
 }
there is no bounds checking.
i.e. for a[4][7] you code may try to access a[3][7]
Thanks that fixed the allocation error!! Now the only issue seems to be that the array isn't changing the value to '!'. I'm assuming I have to delete it and then reconstruct it?
Last edited on
can you post the full code?
This is the full code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;

class Maze {
      
      public: 
              
              int SolveMaze(char **a, int x, int y);
              void DisplayMaze(char **a, int row, int col);
  
              };
void Maze::DisplayMaze(char **a, int row, int col)
{
        for (int i=0; i<col; i++)
        {
             for (int j=0; j<row; j++)
               {
                    cout<<a[j][i];
                    }
                    cout<<endl;
        }
}
int Maze::SolveMaze(char **a, int x, int y)
{
    int i, j;
   if ( i < 0 || i > y - 1 || j < 0 || j > x - 1 ) return false;

     if (a[y][x] != '.')
        return false;
     //Marks as part of solution   
     a[y][x]='!';
     
     if(SolveMaze( a,x,y-1)==true) return true;//North
     if(SolveMaze( a,x+1,y)==true) return true;//East
     if(SolveMaze( a,x,y+1)==true) return true;//South
     if(SolveMaze( a,x-1,y)==true) return true;//West
     //Marks as not part of the solution
     a[y][x]='x';
           
     
     return false;
        
 }
 
 
int main()
{
  
    
    int row, col;
    char paths;
    char **maze = new char*[col];
    
    ifstream infile;
    infile.open("maze.txt");
    
    if(!infile)
    {
      cout<<"ERROR"<<endl;
      exit(1);
      }
    infile >> row >> col;
    
    cout<<row<<col<<endl;
    
   
      //Fills Array from File
      for (int i=0; i<col; i++)
        {
             maze[i]=new char[row];
        }
      for (int i=0; i<col; i++)
        {
             for (int j=0; j<row; j++)
               {  
                    infile>>paths;  
                    maze[j][i]=paths;
                    }
                    }
      
      

  
  Maze laby;
  laby.DisplayMaze(maze, row, col); //Displays maze 
  laby.SolveMaze(maze, 0, 0); //Traverse maze
  laby.DisplayMaze(maze, row, col);//Displays the new Traverse maze
        
        
system("pause");
return 0;
} 
Last edited on
I know the problem is in the SolveMaze() function but not sure why. When I change the value of an element of the vector in main function it works, but not when I change it in the SolveMaze() function.
col has undefined value at line 55.
i figured it out!! The bound was incorrect and I was calling the recursive functions wrong since I forgot the x and y values are swapped for a dynamic array. Thanks for the help :)
Last edited on
Topic archived. No new replies allowed.