This page was exported from Furor Teutonicus [ ]
Export date: Fri Sep 24 11:47:41 2021 / +0000 GMT

C++ multidimensional arrays on the heap

array picture
Picture of the heap array.

When using C++, you cannot store large arrays in the stack, so you should use the heap memory. You do so by e.g. using the new keyword and using a pointer to the reserved memory in the heap.

Problems emerge when you have to use multidimensional arrays. I could not find elegant solutions that matched my personal taste. For example, see the solutions on Stack Overflow. So here is my solution:
struct array {
    int v[42][42];

array * sim = new array;

// initialize int array values to zero
for (int i=0; i<42; i++) {
    for (int j=0; j<42; j++) {
        sim->v[i][j] = 0;

This worked for me and I liked the syntax better than the other solutions, but YMMV.
Excerpt: How to create a C++ big multidimensional array.
Post date: 2012-06-09 17:05:30
Post date GMT: 2012-06-09 16:05:30
Post modified date: 2012-06-09 17:43:52
Post modified date GMT: 2012-06-09 16:43:52
Powered by [ Universal Post Manager ] plugin. HTML saving format developed by gVectors Team