How do I dynamically allocate and deallocate a 2d array?

I don't understand what dynamic or allocation mean. I'm good at producing code that works, but I suck at understanding the vocabulary of what my professor wants. My code works in creating a Tic-Tac-Toe board like the assignment says, but I have no idea if it dynamically allocates a 2d array or not. If it does I'm not sure if it deallocates the code either.
It's a Tic-Tac-Toe Board.

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
// Tic-Tac-Toe Board
// Uses 2D array and dynamically allocates then deallocates the 2D array

#include <iostream>

using namespace std;

int main()
{
    const int ROWS = 3;
    const int COLUMNS = 3;
    char board[ROWS][COLUMNS] = { {'O', 'X', 'O'},
                                  {' ', 'X', 'X'},
                                  {'X', 'O', 'O'} };

    cout << "Here's the tic-tac-toe board:\n";
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
		{
            cout << board[i][j];
		}

        cout << endl;
    }

    cout << "\n'X' moves to the empty location.\n\n";
    board[1][0] = 'X';

    cout << "Now the tic-tac-toe board is:\n";
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
		{
            cout << board[i][j];
		}

        cout << endl;
    }

    cout << "\n'X' wins!";

    return 0;
}
It doesn't. Did you google for "Dynamic allocation C++", or anything similar?

Generally, dynamic allocation happens when you use keyword new. Explaining dynamic allocation is beyond the scope of this post. There are good tutorials online though, although I wouldn't dynamically allocate board for tic-tac-toe.
Last edited on
Topic archived. No new replies allowed.