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

Jun 11, 2016 at 9:49am
Hello, can someone show me a working example please?
Jun 11, 2016 at 9:57am
Jun 11, 2016 at 11:15am
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.
Jun 11, 2016 at 1:14pm
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.
Jun 11, 2016 at 6:09pm
> 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.