0

I am trying to initialize a char** which will act as a two dimensional char array of size (board_size * board_size). However, I am having trouble filling the grid the character '-', when I get to that line I am getting exit code 11 which is a seg fault. How do I assign data to the dynamic 2d array. Is char ** the wrong type to use? What am I missing?

Code:

class World
{
    public:

    World(int num_ants, int num_doodlebugs, int board_size)
    {
        this->board_size = board_size;
        this->num_ants = num_ants;
        this->num_doodlebugs = num_doodlebugs;
        this->board = new char*[board_size*board_size];

        for(int i = 0; i < board_size; i++)
        {

            for(int j = 0; j < board_size; j++)
            {
                this->board[i][j] = '-';
            }
        }

        cout << "Instantiated object" << endl;
    };

    void printWorld()
    {
        cout << "Printing World" << endl;

        for(int i = 0; i < this->board_size; i++)
        {
            for(int j = 0; j < this->board_size; j++)
            {
                cout << this->board[i][j] << " ";
            }

            cout << endl;
        }
    }

    private:
        int num_ants;
        int num_doodlebugs;
        int board_size;
        vector<Ant> ants;
        vector<Doodlebug> doodblebugs;
        char **board;
};
Alex Erling
  • 307
  • 4
  • 19
  • 4
    You can also use `std::vector>` (resizing each element to the right width) or a 1D `char*` or `std::vector` and change how you index – Artyer Oct 14 '20 at 23:44
  • 1
    `board` will be an array of pointers (effectively), but you never make them point anywhere. – Some programmer dude Oct 14 '20 at 23:52
  • As an alternative, [consider a simple matrix class like this](https://stackoverflow.com/a/2076668/4581301). – user4581301 Oct 14 '20 at 23:59
  • 1
    I prefer instantiating a 1d vector of char (of size maxCol * maxRow). I then access the 1d array using a simple function to compute the 1d indx from the 2d row & col. Easy function: " size_t indx1d (size_t row, size_t col) { return ((row * maxCol) + col); } " (C++ can in-line this function.) The memory layout of this 2d array (using std::vector) is an exact match to a 2d array declared in the conventional way, so you can even pass element addresses to external c-functions. Also note - the data is in dynamic memory ... which can handle a fairly large matrix. – 2785528 Oct 15 '20 at 00:12

1 Answers1

2

If you want to do C-style arrays in C++, you need to manage them the same way as you do in C. So if you have a T**, that needs to point at an array of T*, each of which points at an array of T. In your case:

    this->board = new char*[board_size];
    for(int i = 0; i < board_size; i++) {
        this->board[i] = new char[board_size];
        for(int j = 0; j < board_size; j++) {
            this->board[i][j] = '-';
        }
    }

This has the disadvantage that it is not exception-safe, and requires explicit cleanup in the destructor (and more work in copy ctors and assignment operators if you want them). Better to use a std::vector<std::vector<char>>:

    this->board.resize(board_size);
    for(int i = 0; i < board_size; i++) {
        this->board[i].resize(board_size);
        for(int j = 0; j < board_size; j++) {
            this->board[i][j] = '-';
        }
    }
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226