How is it possible to allocate a 2D array on the heap?

Hello, can someone show me a working example please?
http://www.c-faq.com/aryptr/dynmuldimary.html

I recommend the "array2" method: makes life so much easier:

1
2
3
4
5
6
7
8
9
int** array = new int*[ rows ];
array[0] = new int[ rows * cols ];
for (int n = 1; n < rows; n++)
  array[n] = array[0] + n * cols;

...

delete[] array[0];
delete[] array;


It is also possible to do it directly in C++11:
http://stackoverflow.com/a/16239446/2706707

Hope this helps.
closed account (48T7M4Gy)
The double subscripted 2d array structure has the advantage over the single subscripted structure in that it directly corresponds with matrix methods and notations.
> It is also possible to do it directly in C++11:
> http://stackoverflow.com/a/16239446/2706707
legends2k wrote:
Problem with this solution is that the dimensions cannot be a run-time value, but should be known at compile-time.
Topic archived. No new replies allowed.