Declaring a pointer to multidimensional array and allocating the array

84,869

Solution 1

I just found this ancient answer still gets read, which is a shame since it's wrong. Look at the answer below with all the votes instead.


Read up on pointer syntax, you need an array of arrays. Which is the same thing as a pointer to a pointer.

int width = 5;
int height = 5;
int** arr = new int*[width];
for(int i = 0; i < width; ++i)
   arr[i] = new int[height];

Solution 2

const int someheight = 3;
const int somewidth = 5;

int (*array)[somewidth] = new int[someheight][somewidth];

Solution 3

A ready to use example from here, after few seconds of googling with phrase "two dimensional dynamic array":

int **dynamicArray = 0;

// memory allocated for elements of rows. 
dynamicArray = new int *[ROWS];

// memory allocated for  elements of each column.  
for( int i = 0 ; i < ROWS ; i++ ) {
    dynamicArray[i] = new int[COLUMNS];
}

// free the allocated memory 
for( int i = 0 ; i < ROWS ; i++ ) {
    delete [] dynamicArray[i];
}
delete [] dynamicArray;

Solution 4

I suggest using a far simpler method than an array of arrays:

#define WIDTH 3
#define HEIGHT 4

int* array = new int[WIDTH*HEIGHT];
int x=1, y=2, cell;
cell = array[x+WIDTH*y];

I think this is a better approach than an array of an array, as there is far less allocation. You could even write a helper macro:

#define INDEX(x,y) ((x)+(WIDTH*(y)))

int cell = array[INDEX(2,3)];

Solution 5

Personally, my preference is to use a syntactic trick to declare a pointer to the dynamically sized multi-dimensional array. This works in compilers that support Variable Length Arrays (VLAs), which all C++ compilers should, and most current C compilers.

The basic idea is captured in this:

void bar (int *p, int nz, int ny, int nx) {
  int (*A)[ny][nx] = (int(*)[ny][nx]) p;

"p" points at the (contiguous) block of space you want to treat as a multi-dimensional array. "A" has the same value as "p", but the declaration makes the compiler treat references to "A" in the multi-dimensional way you want. For example:

#include <iostream>
using namespace std;

void bar (int *p, int nz, int ny, int nx)
{
  int (*A)[ny][nx] = (int(*)[ny][nx]) p;

  for (int ii = 0; ii < nz; ii++) {
    for (int jj = 0; jj < ny; jj++) {
      for(int kk = 0; kk < nx; kk++) {
          A[ii][jj][kk] = ii*1000000 + jj*1000 + kk;
      }
    }
  }
}


void out (int *p, int nz, int ny, int nx)
{
  int (*A)[ny][nx] = (int(*)[ny][nx]) p;
  cout << A[11][22][33] << endl;
}


int main (void)
{
  int NX = 97;
  int NY = 92;
  int NZ = 20;
  int *space = new int [NZ * NY * NX];

  bar (space, NZ, NY, NX);
  out (space, NZ, NY, NX);
  return 0;
}

Running this produces the output "11022033"

The declaration of the "A" alias is a little weird looking, but it allows you to directly and simply use the desired multi-dimensional array syntax

Share:
84,869

Related videos on Youtube

vince88
Author by

vince88

Updated on July 09, 2022

Comments

  • vince88
    vince88 almost 2 years

    I've tried looking but I haven't found anything with a definitive answer. I know my problem can't be that hard. Maybe it's just that I'm tired..

    Basically, I want to declare a pointer to a 2 dimensional array. I want to do it this way because eventually I will have to resize the array. I have done the following successfully with a 1D array:

    int* array;
    array = new int[somelength];
    

    I would like to do the following with a 2D array but it won't compile:

    int* array;
    array = new int[someheight][somewidth];
    

    The compiler gives me an error stating that ‘somewidth’ cannot appear in a constant-expression. I've tried all sorts of combinations of ** and [][] but none of them seem to work. I know this isn't that complicated...Any help is appreciated.

  • Alexander Aleksandrovič Klimov
    Alexander Aleksandrovič Klimov over 13 years
    Did you try this? It doesn't compile (if height and width are both variables)
  • vince88
    vince88 over 13 years
    Yes I have tried this and still get the same error with the compiler. it will say that "‘width’ cannot appear in a constant-expression"
  • Alexander Rafferty
    Alexander Rafferty over 13 years
    This is an array of arrays, not really a 2D one.
  • Arun
    Arun over 13 years
    @Alexander Rafferty: I see -- what is the difference between "array of arrays" and "2D arrays'?
  • vince88
    vince88 over 13 years
    If I were to access an element of this array would it just be arr[width][height]?
  • vince88
    vince88 over 13 years
    Awesome. Thanks for the help.
  • Stephen Lin
    Stephen Lin over 11 years
    @ArunSaha, this is old, but to answer your question and for anyone else that stumbles across this, a 2-D array is contiguous in memory; a dynamic array of dynamic arrays is contiguous in the first dimension, but each array in the second dimension is stored separately
  • Arun
    Arun over 11 years
    @Stephen Lin: Appreciate your explanation! Say there is a also int staticArray[ ROWS ][ COLUMNS ];. Also assume that there is a function printArray( int arr[ ROWS ][ COLUMNS ] ) which access and prints all the elements as arr[ i ][ j ]. One could pass either staticArray or dynamicArray to that function and it would work, correct? There is structural difference and your point is valid. However, since there is no behavioral difference, I tend to consider the difference as a implementation detail.
  • Stephen Lin
    Stephen Lin over 11 years
    @ArunSaha, no that's the point, you can pass int[N] (array of N integers) as int * (pointer to integer) but you can't pass int[R][C] (array of R of array of C integers) as an int ** (pointer to pointer to integer); there is no implicit conversion and if you cast it to make it work you'll corrupt memory; int [R][C] decays to int (*)[C] (pointer to array of C integers) instead...see this answer
  • Stephen Lin
    Stephen Lin over 11 years
    @ArunSaha just think about how element staticArray[x][y] would be accessed versus dynamicArray[x][y] by the compiler...the first is *(&staticArray[0][0] + (x * C + y)) but the second is *(*(&dynamicArray[0] + x) + y); not equivalent at all
  • Arun
    Arun over 11 years
    @Stephen Lin: I agreed and admitted that there indeed is structural difference, the memory layout is certainly different. The point I am trying to convey is that, from a client's (e.g. printArray()), there is no difference. (In other words, the difference is abstracted out). Agree or disagree? [I consider the situation similar to when I create a Array2d class, and provide a member function getElement( int i, int j), the user is not expected to know how getElement() is implemented.]
  • Stephen Lin
    Stephen Lin over 11 years
    @ArunSaha what I'm saying is the difference is not abstracted out, the types are not equivalent and passing a dynamic array int** as int[R][C] will not work.
  • James Johnston
    James Johnston over 10 years
    Unlike the other answers here, this one actually answers the question, which was how to declare a pointer to a multidimensional array - not a jagged array. This is an important differentiation for performance-sensitive applications. It works if "somewidth" is a constant, and only the "someheight" changes. If you look at how this 2D array is laid out in memory - it's all together and contiguous, with no more than the single "new" memory allocation done. The answer by Alexander Rafferty is also good if the width is also dynamic. The other answers are slow because they do a ton of mallocs...
  • Krishna Oza
    Krishna Oza over 9 years
    It is very well specified that when declaring a 2D-Array the row argument can be a variable , but the column argument has to be constant only.
  • Bregalad
    Bregalad over 9 years
    No, array of array and array of pointers are not the same thing. Why is this post accepted ? The answer of Tony The Lion is the correct one, this one is completely wrong.
  • Dicer
    Dicer over 5 years
    How do you properly destroy this array? delete[] array; I assume so since it's really a contiguous piece of memory.
  • luca
    luca about 4 years
    Just to clarify James Johnston's important note. Arrays and pointers are not the same: an array can decay into a pointer, but a pointer doesn't carry state about the size/configuration of the data to which it points. This prevents using pointers of pointers as a contiguous memory. Still the decay of an N dimensional array to a pointer to N-1 dimensional array is allowed by c++ since you can lose the leftmost dimension and still being able to correctly access array elements with N-1 dimension information. Details in stackoverflow.com/a/3925968/1201614