Populating Dynamic 2D Array

Hello. I am trying to create a 2D array, and I'm almost there but can't figure out the syntax/implementation for what I want to do.

In my program I am accepting values that will define the number of columns and rows as a command line argument.

Basically, my dynamic array will accept the value for columns, but I can't figure out how to now implement rows.

I am trying to form a 2D grid of values.

Any help is graciously accepted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

 10
 11 int main(int argc, char* argv[]){
 12
 13    int num_players = atoi(argv[1]);    
 14    int cols = atoi(argv[2]);
 15    int rows = atoi(argv[3]);
 16    char player;              
 17
 18    char** board_ar = new char*[cols];
 19
 20    for(int i =0;i<cols;i++){
 21       board_ar[i]                   // populate board_ar with rows


I started to make a for loop at the end, but I'm not sure what I want to do with it.
Last edited on
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
//A
char** board_ar = new char*[rows];
for(int i=0; i<rows; ++i)
   board_ar[i] = new char[cols];

board_ar[r][c]; //access cell at row `r', col `c'

//releasing the memory
for(int i=0; i<rows; ++i)
   delete [] board_ar[i];
delete [] board_ar;


//B
char **board_ar = new char*[rows];
board_ar[0] = new char[rows*cols];
for (int K=1; K<rows; ++K)
   board_ar[K] = board_ar[0] + K*cols;

board_ar[r][c]; //access cell at row `r', col `c'
//releasing the memory
delete [] board_ar[0];
delete [] board_ar;

//C
std::vector< std::vector<char> > board_ar (rows, std::vector<char>(cols));
board_ar[r][c]; //access cell at row `r', col `c'
//automatic memory management

//D
std::vector <char> board(rows*cols);
board[r*cols + c]; //access cell at row `r', col `c' 

notice the differencea in the memory layout
in B and D, you have one big chunk with all the cells, the rows are contiguous (the cell b[K][cols-1] is next to b[K+1][0])
in A and C every row is a separated chunk
Last edited on
Thank you for your reply, ne555.

I tried implementing your //B solution, and I'm getting a segmentation fault, and I'm not really sure what the meaning of this is, and how to get around it.
I think it has something to do with allocating memory on the heap, but I don't know why this would be a problem in the code I have.


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

#include <iostream>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <string>
  6
  7
  8 using namespace std;
  9
 10 int main(int argc, char* argv[]){
 11
 12    int num_players = atoi(argv[1]);    // Number of players
 13    int cols = atoi(argv[2]);
 14    int rows = atoi(argv[3]);
 15    char player;              // Current player
 16
 17    char** board_ar = new char*[rows];
 18    board_ar[0] = new char[rows*cols];
 19
 20    for(int i =0;i<rows;i++){
 21       board_ar[i] = board_ar[0] + i*cols;                   // populate board_ar with rows
 22    }
 23
 24    for(int i=0; i<rows;i++){
 25       for(int j=0;j<cols;j++){
 26          cout << board_ar[i][j];
 27       }
 28       cout << endl;
 29    }
can't reproduce, ¿what parameters are you using?
also
- your program is incomplete, it's missing the main()'s closing brace
- you just reserve memory, never put any value in the cells. For example, with
1
2
3
for(int K=0; K<rows; ++K)
   for(int L=0; L<cols; ++L)
      board_ar[K][L] = '*';

- you didn't release the memory
I think it's working now. I was just forgetting to put in command line arguments.
Thank you for also pointing out that my code is only reserving memory.
ne555,

There's just one more thing that I'm wondering.

I know that it is prudent to delete any dynamically allocated data after you don't need it anymore within your program.

In the case above, is the correct way to delete the dynamic array:

 
delete[][] board_ar;


Thank you so much for your help.
It's a life saver right now. :)
delete[][] board_ar;
¿does that even compile?
I've already showed you how to do it
Topic archived. No new replies allowed.