Furor Teutonicus blog | over | volg | teuto | lyme | archief | doneer | todo
🕰️
  ⬩  
✍️ Evert Mouw
  ⬩  
⏱️ 0 min

C++ multidimensional arrays on the heap

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.


Deze blogpost werd in december 2022 overgezet van WordPress naar een methode gebaseerd op Markdown; het is mogelijk dat hierbij fouten of wijzigingen zijn ontstaan t.o.v. de originele blogpost.