you can have this idea with vectors, which use dynamic arrays inside.
that is the better way, but
int *array = new int[5] { 9, 7, 5, 3, 1 }; // initialize a dynamic array since C++11
#include <iostream>
usingnamespace std;
int main()
{
int n = 4;
auto a1d = newint[4]; // OK
delete[] a1d;
auto b1d = newint[n]; // OK
delete[] b1d;
auto A2d = newint[4][4]; // OK, but new doesn't seem necessary
delete[] A2d;
auto B2d = newint[n][n]; // not legitimate
delete[] B2d;
auto C2d = newint*[n];
for ( int i = 0; i < n; i++ ) C2d[i] = newint[n]; // OK, but painful
for ( int i = 0; i < n; i++ ) delete C2d[i];
delete [] C2d;
}
Hi @coder777, it was for my own interest (in response to @JLBorges' post), so if you can give an explanation I'd appreciate it.
I had been used to allocating 2-d array memory in the way that I had done for C2d (on the rare occasions that I use it - I usually prefer to flatten to 1-d arrays for MPI transfers etc.) I was surprised that @JLBorges' solution worked, but then equally surprised that it didn't then work for run-time array bounds. There doesn't seem much purpose in using new for fixed array sizes, other than access to large amounts of heap space.