-2

For an assignment, I had to separate some code for Conway's Game of Life into multiple files, but now I'm trying to get a deeper understanding of why the code was written the way it was. I have a class, World, and part of the constructor for that class looks like this:

world.h:

class World {
public:
    // Constructor/destructor
    World();
    ~World();
    
    void updateWorld();

    ...

private:    
    char **m_grid_1;
    char **m_grid_2;

    ...
};

world.cpp:

World::World()
{
    m_toggle = true;

    m_grid_1 = new char*[MAX_ROWS];
    m_grid_2 = new char*[MAX_ROWS];

    for (int i = 0; i < MAX_ROWS; i++) {
        m_grid_1[i] = new char[MAX_COLS];
        m_grid_2[i] = new char[MAX_COLS];
    }

    ...
}

MAX_ROWS and MAX_COLS are globals that live in a header now.

Arrays have always kind of confused me, so these questions might seem kind of basic:

  1. What does char** mean in the context of the member variable m_grid_1 or m_grid_2? Pointer to a pointer of chars? Those members are 2D arrays, so I'm not sure why pointers are used here instead.

  2. Why is new used here in combination with that loop in the constructor? Couldn't you just say m_grid_1[MAX_ROWS][MAX_COLS];?

Also, rather than toggle between the two grids, if I wanted to make a local copy of m_grid_1 in the member function updateWorld() and use that local copy to get the next state of the game, would I need to use new? This is what I have now, and it seems to work, but I'm not sure if I'm doing something wrong by not dynamically allocating the 2D array here:

void World::updateWorld() {
    char grid_copy[MAX_ROWS][MAX_COLS];

    for (int i = 0; i < MAX_ROWS; i++) {
        for (int j = 0; j < MAX_COLS; j++) {
            grid_copy[i][j] = m_grid_1[i][j];
        }
    }
  
  // loop over grid_copy to find next state of m_grid_1
  
}
dylosaur
  • 167
  • 1
  • 10
  • The code itself is not good. The code uses c-style arrays in a c++ environment. Try to get a better understanding how to create static c-style arrays and the difference to dynamic arrays. Then you will learn why you need the pointers and why your approach does not work – RoQuOTriX Mar 03 '21 at 06:59
  • This might help [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). Its a long way from that q&a to understand why the arrays need to be dynamically allocated or not, though thats covered in the answer you got – 463035818_is_not_a_number Mar 03 '21 at 07:55
  • @RoQuOTriX I didn't write the main part of this code, I'm just trying to understand it. – dylosaur Mar 03 '21 at 08:00
  • @dylosaur yes that is ok, I wanted to say that the code itself is not good and I hope it would not pass a code review if there is no other restriction – RoQuOTriX Mar 03 '21 at 08:36

1 Answers1

3

Do all arrays in C++ classes have to be dynamically allocated?

Not in general.

or just their member variables?

Member variables don't have to be allocated dynamically either.

  1. What does char** mean ... Pointer to a pointer of chars?

It is a pointer to pointer to char.

Those members are 2D arrays

Correction: Those members are pointers. They used pointers, therefore they are not arrays.

  1. Why is new used here in combination with that loop in the constructor?

Because the author of the program chose to use it.

Couldn't you just say m_grid_1[MAX_ROWS][MAX_COLS];?

Maybe. If MAX_ROWS and MAX_COLS are compile time constant and if they are small, then sure.

If I wanted to make a local copy of m_grid_1 in the member function updateWorld() and use that local copy to get the next state of the game, would I need to use new?

If the question is whether you need dynamic allocation, then maybe. See the answer above.

There is hardly ever need to use new. A simpler and safer way to create a dynamic array is to use std::vector.

eerorika
  • 181,943
  • 10
  • 144
  • 256
  • Your answer is very detailed and answers every aspect of the question. But I think the answer will not help the user because he is missing basic concepts of c/c++ programming – RoQuOTriX Mar 03 '21 at 07:01
  • 2
    `std::array` is also a good candidate because sizes are known at compile-time. The question is just: how big would be the resulting arrays? If small enough, as you said, it's fine not to use dynamic allocation. – Bktero Mar 03 '21 at 07:02
  • @eerorika The constants are hard-coded, which is part of why I was confused about the dynamic allocation. MAX_ROWS and MAX_COLS are just 20 in the example, but I could change them to anything. Would dynamic allocation be better for larger sized grids? Also, I like vectors, but this was just part of the assignment and I wanted to try to match it closely and learn about why it was written the way it was. – dylosaur Mar 03 '21 at 08:09