Dynamically allocating a 2D array of object pointers in a class

I'm struggling at the moment with the idea of dynamically allocating arrays at runtime. Coming from Java, used to just declaring the arrays in the class skeleton and only needing the size in the implementation.

This is what I've found to dynamically allocate 2D arrays:

Grid.h   
Block** grid;

Grid.cpp
grid = new Block*[size]
for(int i = 0 ; i < size ; i++)
    grid[i] = new Block[size]

This works pretty okay, although dealing with objects I've always been told that using pointers to objects rather than storing the objects themselves is much better performance wise. So when I tried to make the second dimension of arrays pointers like this:

Grid.cpp
grid = new Block*[size]
    for(int i = 0 ; i < size ; i++)
        grid[i] = new Block*[size];

When I changed my code to this, I got an error:

error: assigning to 'Block *' from incompatible type 'Block **'; dereference with *
        grid[i] = new Block* [size];
                ^ ~~~~~~~~~~~~~~~~~
                  *

Being slightly new to the C++ ways of doing things, can someone tell me what I'm doing wrong? Or even if I'm trying to do the wrong thing entirely?

Thanks in advance!


A dynamic 2D array is an array of pointers to arrays. You should initialize first the array of pointer, then the others array using a loop.

Here an example using int that creates an array[rowCount][colCount]:

int** array = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
    array[i] = new int[colCount];

otherwise of course you can always have a 2D array on the stack by using:

int array[rowCount][colCount];

我不会推荐你写这种类型的代码,但如果你仍然想破解你的出路,你可以做这样的事情: -

int main()
{
Block*** grid;      

grid = new Block**[10];         
for(int i = 0 ; i < 10 ; i++)
{       
    grid[i] = new Block*[10];   
}

/*Here, we have created a grid of pointers*/

/*

|Block**[10]|

|Block[0] **|------------->|Block* [10]|
|Block[1] **|------------->|Block* [10]|
|Block[2] **|------------->|Block* [10]|
|Block[3] **|------------->|Block* [10]|
..
..
|Block[9] **|------------->|Block* [10]|

*/

for(int i = 0 ; i < 10 ; i++)
{       
    for(int j = 0 ; j < 10 ; j++)
    {
        grid[i][j] = new Block[10]; 
    }
}

/*

|Block**[10]|

|Block[0] **|------------->|Block* [0]|------------->|Block1|Block2| .. |Block10|
                           |Block* [1]|------------->|Block1|Block2| .. |Block10|
                           |Block* [2]|------------->|Block1|Block2| .. |Block10|
                           ..
                           |Block* [9]|------------->|Block1|Block2| .. |Block10|


|Block[1] **|------------->|Block* [0]|------------->|Block1|Block2| .. |Block10|
                           |Block* [1]|------------->|Block1|Block2| .. |Block10|
                           |Block* [2]|------------->|Block1|Block2| .. |Block10|
                           ..
                           |Block* [9]|------------->|Block1|Block2| .. |Block10|          
|Block[2] **|
|Block[3] **|
..
..
|Block[9] **|

*/
 }

使用2d数组的线性表示形式:

std::unique_ptr<int[]> array(new int[rowCount*colCount]);

for (size_t r = 0; r < rowCount; r++)
  for (size_t c = 0; c < colCount; c++)
    (array.get())[r*colCount + c] = r*c;
链接地址: http://www.djcxy.com/p/72924.html

上一篇: C ++类中的动态内存分配

下一篇: 在类中动态分配对象指针的二维数组