Skip to main content
C++ VLA support *does* allow new, just not new[]. Link another answer here, instead of the C answer.
Source Link
Peter Cordes
  • 351.6k
  • 49
  • 674
  • 927

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

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

See this answer. Even compilers Compilers like gcc that allow variable-length arrays as an extension to C++ don't allow a 2ndcan use new as shown here to get fully runtime-variable array dimension that isn't a constantfunctionality like C99 allows, so this isn't a full replacement for what you can do in Cbut 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 better optimization especially when the same compiler needs to checkoptimizations as a real 2D array (e.g. proving or provechecking 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:

enter image description here

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

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

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

See this answer. Even compilers like gcc that allow variable-length arrays as an extension to C++ don't allow a 2nd dimension that isn't a constant, so this isn't a full replacement for what you can do in C.

Another efficient option is to do the 2d indexing manually into a big 1d array, as another answer shows, allowing better optimization especially when the compiler needs to check or prove that arrays don't alias each other.


Otherwise, you can use an array of pointers to arrays to allow syntax like 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:

enter image description here

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:

enter image description here

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

Mentioning the other options seems appropriate for this highly-voted FAQ answer. Also don't claim that this is truly a 2D array. It only allows similar syntax.
Source Link
Peter Cordes
  • 351.6k
  • 49
  • 674
  • 927

A dynamic 2D arrayIf your row length is basicallya compile time constant, C++11 allows

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

See this answer. Even compilers like gcc that allow variable-length arrays as an extension to C++ don't allow a 2nd dimension that isn't a constant, so this isn't a full replacement for what you can do in C.

Another efficient option is to do the 2d indexing manually into a big 1d array, as another answer shows, allowing better optimization especially when the compiler needs to check or prove that arrays don't alias each other.


Otherwise, you can use an array of pointers to arrays to allow syntax like 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:

enter image description here

A dynamic 2D array is basically an array of pointers to arrays. 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:

enter image description here

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

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

See this answer. Even compilers like gcc that allow variable-length arrays as an extension to C++ don't allow a 2nd dimension that isn't a constant, so this isn't a full replacement for what you can do in C.

Another efficient option is to do the 2d indexing manually into a big 1d array, as another answer shows, allowing better optimization especially when the compiler needs to check or prove that arrays don't alias each other.


Otherwise, you can use an array of pointers to arrays to allow syntax like 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:

enter image description here

change the array name to be compatable with the image. And deleting the should word, because the stated methodis not a one to advise other with
Source Link

A dynamic 2D array is basically an array of pointers to arrays. You shouldcan initialize it using a loop, like this:

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

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

enter image description here

A dynamic 2D array is basically an array of pointers to arrays. You should initialize it using a loop, like this:

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

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

enter image description here

A dynamic 2D array is basically an array of pointers to arrays. 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:

enter image description here

added 11 characters in body
Source Link
gsamaras
  • 73k
  • 46
  • 200
  • 321
Loading
added 14 characters in body
Source Link
user366312
  • 16.4k
  • 68
  • 251
  • 478
Loading
swap sizeX and sizeY
Source Link
Mehrdad Afshari
  • 419.5k
  • 92
  • 857
  • 793
Loading
a pic I made for visualizing what you said. If you like it, accept it, you know better than me!
Source Link
Loading
corrected the terminology...
Source Link
Mehrdad Afshari
  • 419.5k
  • 92
  • 857
  • 793
Loading
Source Link
Mehrdad Afshari
  • 419.5k
  • 92
  • 857
  • 793
Loading