How to Declare a 2D Array in C++ Using New

How do I declare a 2d array in C++ using new?

If your row length is a compile time constant, C++11 allows

auto arr2d = new int [nrows][CONSTANT];

See this answer. Compilers like gcc that allow variable-length arrays as an extension to C++ can use new as shown here to get fully runtime-variable array dimension functionality like C99 allows, but portable ISO C++ is limited to only the first dimension being variable.

Another efficient option is to do the 2d indexing manually into a big 1d array, as another answer shows, allowing the same compiler optimizations as a real 2D array (e.g. proving or checking that arrays don't alias each other / overlap).


Otherwise, you can use an array of pointers to arrays to allow 2D syntax like contiguous 2D arrays, even though it's not an efficient single large allocation. You can initialize it using a loop, like this:

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

The above, for colCount= 5 and rowCount = 4, would produce the following:

Sample Image

Don't forget to delete each row separately with a loop, before deleting the array of pointers. Example in another answer.

Initializing a 2D array with operator new

If you want a pointer to an array on heap, you use a pointer to the first element. This means it's the same type as if you would allocate a single element:

uint64_t* p1 = new uint64_t;
uint64_t* p2 = new uint64_t[2];

You should one the second line only use p and p+1, because p+2 is in this example out of range. This goes for all examples below WITH BRACKETS as well.

If you create a pointer to a multidimensional array, you also use a pointer to the first element, but the notation slightly changes. You don't always need the parentheses, but I left them in to see the patterns clearly:

uint64_t (*p1) = new uint64_t; // Optional parentheses.
uint64_t (*p2) = new uint64_t[2]; // Optional parentheses.
uint64_t (*p3)[3] = new uint64_t[2][3]; // Required parentheses.
uint64_t (*p4)[3][4] = new uint64_t[2][3][4]; // Required parentheses.

As you can see, we need parentheses when there are brackets.

And now with pointers:

uint64_t *(*p1) = new uint64_t*; // Optional parentheses.
uint64_t *(*p2) = new uint64_t*[2]; // Optional parentheses.
uint64_t *(*p3)[3] = new uint64_t*[2][3]; // Required parentheses.
uint64_t *(*p4)[3][4] = new uint64_t*[2][3][4]; // Required parentheses.

And finally, BONUS round:

uint64_t **(*p1) = new uint64_t**; // You can leave out the parentheses.
uint64_t **(*p2) = new uint64_t**[2]; // You can leave out the parentheses.
uint64_t **(*p3)[3] = new uint64_t**[2][3];
uint64_t **(*p4)[3][4] = new uint64_t**[2][3][4];

Malloc a 2D array in C [duplicate]

like this : int (*arr)[M] = malloc(sizeof(int[N][M]));

arr is pointer to int[M].

use like arr[0][M-1];

and free(arr);

Initializing Dynamic 2d array in c++

It seems p[i][j] = **ref; should be p[i][j] = ref[i][j];.

Also you should follow The Rule of Three. In other words, you should declare copy constructor and assignment operator to handle object (including pointers) copying properly.

Dynamically Allocated input, and output 2-D Arrays in C++

I solved it:

#include <iostream>
//#include <vector>

using namespace std;

int main() {
int row, col;
cout << "Please enter the rows size: " << endl;
cin >> row;
cout << "Please enter the column size: " << endl;
cin >> col;

cout << "Please enter the numbers you want to put into a 2D array (it should
look like a matrix graph)." << endl;
cout << "Press enter after each number you input: " << endl;

int** map = new int*[row];
for (int i = 0; i < row; ++i)
map[i] = new int[col];

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> map[i][j];
}

}

cout << endl;
//Print
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << map[i][j] << " ";
}
cout << endl;

}
cout << endl;


// DON'T FORGET TO FREE
for (int i = 0; i < row; ++i) {
delete[] map[i];
}
delete[] map;
system("pause");
return 0;
}

How to allocate memory to a 2D array of objects in c++? [duplicate]

As already was mentioned, here is nice post where the general answer to the question was detailing explained.
How do I declare a 2d array in C++ using new?

In your case, if you want to store this as 2D array. You should allocate first all rows, where each row is a ZooObject**, which is ZooObject `s pointers array.
And after, for each of the row, you should allocate the array (columns) of ZooObject*. You will have something like this:

    Zoo(int rows, int cols) {
this->rows = rows;
this->cols = cols;

zooArray = new ZooObject**[rows];
for (int i = 0; i < rows; ++i) {
zooArray[i] = new ZooObject*[cols];
for (int j = 0; j < cols; ++j) {
zooArray[i][j] = nullptr;
}
}
}

However, consider using 1D arrays, you still can access it via 2 dimensions, via corresponding method, which converting rowId, colId pair to 1D dimension.

Also, don't forget to delete which you new!



Related Topics



Leave a reply



Submit