How do I make a pointer to a multidimensional array which has an unknown size?

how do I make a pointer to a multidimensional array, which have a unknown size? I've tried this:

int **triangles;

triangles = new int[numTriangles][3];

But i get this error:

cannot convert 'int (*)[3]' to 'int**' in assignment

Upvotes: 1

Views: 2650

Answers (4)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507373

Well, the error message says it :) If you 'new an array, then new returns a pointer to its first element.

A multidimensional array is just another name for an array of arrays. You 'newed an array of int[3], and precisely numTriangles elements of those. So what new does is returning a pointer to int[3]. The variable you assign the result to has to have that type, which is a pointer to an array of 3 integers:

int (*triangles)[3];
triangles = new int[numTriangles][3];
triangles[triangleIndex][vertexIndex] = 42;

Of course, typedefs also help here:

typedef int triangles_t[3];
triangles_t * triangles = new int[numTriangles][3];

The parentheses are needed because of the C++ precedence rules. They tell the compiler that it's not creating an array of three pointers to integers, but a pointer to an array of 3 integers. This is similar to the more common use on function pointers ("void (*funptr)()")...

Upvotes: 4

Scottie T
Scottie T

Reputation: 12245

To do this, you will have to loop over one dimension and allocate space for the other dimension:

int** triangles;
triangles = new int*[NUMROWS];

for (int i = 0; i < NUMROWS; i++) {
   triangles[i] = new int[NUMCOLS];
}

But be careful when doing it this way, you'll have to do the same to deallocate the memory.

for (int i = 0; i < NUMROWS; i++) {
   delete [] triangles[i];
   triangles[i] = 0;
}
delete [] triangles;
triangles = 0;

Upvotes: 4

KenE
KenE

Reputation: 1805

Multidimensional arrays in c++ are just syntactic sugar.

int a[n][m];

is equivalent to

int a[n*m];

so you just need a regular pointer - i.e.:

int *triangles;
triangles = new int[numTriangles*3];

Upvotes: 1

Iraimbilanja
Iraimbilanja

Reputation:

triangles = new int[numTriangles*3];

Then access it as:

triangles[numTriangles*triangleIndex+vertexIndex] = blah;

But this is tedious and error prone so I suggest using boost.multi_array instead, or rolling your own (really simple):

template<class T>
class Matrix {
public:
    Matrix(int _w, int _h) : data(_w*_h), w(_w), h(_h) {
    }

    T      & operator()(int x, int y)       { return data[y * w + x]; }
    T const& operator()(int x, int y) const { return data[y * w + x]; }

private:
    std::vector<T> data;
    int w, h;
};

// usage:
int main() {
    Matrix<float> triangles(numTriangles, 3);
    triangles(triangleIndex, vertexIndex) = blah;
}

If, on the other hand, you actually want an array of triangles, rather than a twodimensional array, just use a vector<Triangle> where Triangle is a class :)

Upvotes: 4

Related Questions