2D dynamic array allocation from text file

Trying to program a maze to read in a text file and place it into a 2D dynamic array. The first line is the dimensions of the maze and the next part is the actual maze itself. I thought I declared the 2D array correctly, and it compiles, but I keep getting a weird error message when I try to run it

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted

Not sure what this is indicating within my code or how I would fix it. Any assistance would be much appreciated.
Below is my code...

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <deque>

using namespace std;

int nRows, nCols = 10;

int main()
{
    ifstream infile;

    infile.open ("maze10.txt");

    
    int nRows, nCols; // Size needed for array
    string symbols;   // symbols used for maze

    int** a = NULL; // Pointer to int, initialize to NULL

    infile >> nRows >> nCols;   // Read in size of maze
    
    a = new int*[nRows];


    while(!infile.eof())
    {
      int **a = new int*[nRows];
      for (int i = 0; i < nRows; ++i)
        {
          a[i] = new int[nCols];
        }
        
   }
    for(int i = 0; i < nRows; ++i) 
    {
     delete [] a[i];
    }
    delete [] a;

}
Last edited on
One reason your code doesn't seem so obvious to you is maybe because you might be having a little trouble understanding pointer syntax. Maybe instead of using a bunch of pointers to arrays, you can use a vector class.

Example:

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
#include <vector>
#include <iostream>
#include <fstream>
#include <string>

void readSize(std::ifstream &in, int &width, int &height)
{
    in >> width >> height;
}

std::vector<int> createArray(int width, int height)
{
    std::vector<int> vec;
    vec.reserve(width * height); // To hold all of the rows and columns
    return vec;
}

int getFromMaze(std::vector<int> &maze, int width, int x, int y)
{
    return maze.at(width*y + x);
}

int main(int argc, char **argv)
{
    // Open maze file
    ifstream infile;
    infile.open("maze10.txt");

    // Get width and height
    int width, height;
    readSize(infile, width, height);

    // Load maze
    std::vector<int> maze = createArray(width, height);
    int index = 0;
    while (!infile.eof())
    {
        int nextObject;
        infile >> nextObject;
        maze.at(index) = nextObject;
        ++index;
    }

    // Index a position in the maze
    int value = getFromMaze(maze, width, height, 3, 2);
    std::cout << "3rd column, 2nd row: " << value << '\n';
}


Not sure how much this helps, but maybe you should review your syntax for pointers and you will understand better.
I will try reviewing pointer syntax for this. I was going to try using vectors, but I am required to use 2D array. I appreciate your response.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
   int** a = NULL;      // a #1

    infile >> nRows >> nCols;   // Read in size of maze
    
    a = new int*[nRows];


    while(!infile.eof())                   // if this is true once, it will always be true
    {                                      // endless loop endlessly allocating memory...  no wonder bad alloc.
      int **a = new int*[nRows];           // a #2.. not the same as line 22.
      for (int i = 0; i < nRows; ++i)
        {
          a[i] = new int[nCols];
        }
        
   }                                       // a #2 stops existing here, all memory allocated for it is leaked.
    for(int i = 0; i < nRows; ++i) 
    {
     delete [] a[i];            // deleting pointers that weren't obtained through new: undefined behavior.
    }
    delete [] a;
I got it to read in the file and print it out, although it does print out the last line in the file one more time than I need it to, but I can fix that later. How will 'eof()' always be true if it is true once? Won't it loop back up to that while statement until it finds the end of the file?
Last edited on
Won't it loop back up to that while statement until it finds the end of the file?

In the loop above, you do not read from the file. If you don't encounter an eof condition because you never read from the file, the condition will always be true.
oh ok. Sorry after I posted I modified my code and put

1
2
3
    infile >> nRows >> nCols;   // Read in size of maze
    cout << "nRows, nCols: " << nRows  << "," << nCols;


in the while loop and got it working. Now i just need to figure out how to implement a queue in order to traverse the maze
Topic archived. No new replies allowed.