0

I want to create a 2D pointer boolean array, but as soon as I declare it and try to initialize it I get a segmentation fault. I tried only declaring the array and not initializing it, and I've tried initializing the array as well.

I have some functions and global variables in my program, and have tried making everything except my main function, #include , and using namespace std; into a comment but I'm still getting the error: only declaring:

int main(){
    // Variable declarations. You can add more if necessary
    bool **world;
    int nrows, ncols;
    char next;

    cout << "Enter world dimensions (rows and columns): ";
    cin >> nrows >> ncols;


    **world = new bool*[ncols];
    for (int   i = 0; i < ncols; i++) {
      world[i] = new bool[nrows];
    }   

declaring and initializing:

int main(){
    // Variable declarations. You can add more if necessary
    bool **world;
    int nrows, ncols;
    char next;

    cout << "Enter world dimensions (rows and columns): ";
    cin >> nrows >> ncols;


    **world = new bool*[ncols];
    for (int   i = 0; i < ncols; i++) {
      world[i] = new bool[nrows];
    }   
    for (int i = 0; i < ncols; ++i) {
      for (int j = 0; j < nrows; ++j) {
        world[i][j] = false;
      }
    } 

The error is: Segmentation fault (core dumped).

lukehod
  • 408
  • 4
  • 14
MegaHertz
  • 29
  • 3
  • 3
    In C++ you should steer towards solutions using `std::vector` and well away from manual allocations using `new[]`. – tadman Feb 11 '20 at 03:15
  • 7
    `**world = new bool*[ncols];` -- this doesn't do what you think it does. Does your C++ compiler warn you about this line? – Sam Varshavchik Feb 11 '20 at 03:15
  • unfortunately my compiler isn't giving me any warnings, only the error message. I'm curious what that code does in actuality? – MegaHertz Feb 11 '20 at 03:20
  • 1
    It would behoove you to implement a simple (i.e. one-dimensional) array before you attempt this. – Beta Feb 11 '20 at 03:20
  • It's for an assignment, unfortunately it is necessary for the assignment to use a 2D array – MegaHertz Feb 11 '20 at 03:22
  • 2
    In addition to what @SamVarshavchik said, many compilers let you tune what warnings they produce. For example, for `gcc` you could pass `-Wall` to see all warnings and `-Werr` to treat any warning as an error. Anyway, as @SamVarshavchik implies the fix is `world = new bool*[ncols];` - you want to make `world`, point to the newly allocated array of pointers to boolean. – Oliver Dain Feb 11 '20 at 03:28
  • 1
    Perhaps this question will have some answers that would help: [https://stackoverflow.com/questions/16001803/pointer-to-pointer-dynamic-two-dimensional-array](https://stackoverflow.com/questions/16001803/pointer-to-pointer-dynamic-two-dimensional-array) – lukehod Feb 11 '20 at 03:33
  • 1
    `**world = new bool*[ncols];` should give an error message, if you don't see one then adjust your compiler settings – M.M Feb 11 '20 at 03:41
  • Thank you all, will be adjusting my compiler and the solution works – MegaHertz Feb 11 '20 at 03:46
  • Also note - per your prompt, you are using `ncols` in place of `nrows` and vice-versa throughout your code. If you want a `rows x cols` dynamic array of `bool`, you need: `world = new bool*[nrows];` and then loop allocating `world[i] = new bool[ncols];`. Otherwise you are creating a `ncols x nrows` collection of `bool`. – David C. Rankin Feb 11 '20 at 06:24

1 Answers1

1

**world = new bool*[ncols]; is equivalent to world[0][0] = new bool*[ncols]; as world is uninitialised this is undefined behaviour. This only complies as a pointer is convertible to bool. If your array was a different type like int your code wouldn't compile.

What you actually want to do is assign to the pointer:

world = new bool*[ncols];

A std::vector would make your code simpler and safer, this single line replaces all your initialisation code and has no memory leaks:

std::vector<std::vector<bool>> world(ncols, std::vector<bool>(nrows, false));

I'm general multidimensional arrays and vectors have poor performance, you are better off using a single dimensional array and calculating the index with: col * nrows + row

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60