dynamic 2D arrays

This is not really an opengl question but I im using openGl in this particular program. I am having trouble dynamically allocating a 2d array with c++ this is what i have done
at the top i have done this int **FaceData
FaceData = new int[num_polygons][num_verticies*3];
why doesnt this work?

It won’t work becasue a pointer and an array are different things.

With int **FaceData, FaceData[0] is a pointer to an integer. With int FaceData[5][10] (numbers are just an example), FaceData[0] is an array of five integers.

If you want dynamic 2D arrays, where both dimensions are dynamic, you have to like this.

int **FaceData;

FaceData = new int [num_polygons];
for(int i = 0; i < num_polygons; ++i)
FaceData[i] = new int[num_vertices
3];

Yeah your right. Thanks that helps

You should also be aware that if you use dynamic 2d arrays, you won’t be able to use them with the vertex array functions. That is because the vertex array functions assume the data to all be contiguous, and that won’t likely be the case with dynamic 2d arrays. (You allocate memory for an array of pointers first, then allocate memory for each of those pointers.)

so what your saying is. this wont work

lets say i have something like this
FaceData[100][100];

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, VertexData);
glDrawElements(GL_POINTS, 4, GL_INT, FaceData[0]);

I think Deiussum meant the dynamic 2d array from Bob

However you can make a memory contiguous dynamic 2d array (which will work with gl functions):

int ** FaceData;
int * FaceData_Memory;

FaceData = new int * [num_polygons];
FaceData_Memory = new int [num_polygons * num_vertices * 3];

for (size_t i = 0; i < num_polygons; ++i)
FaceData[i] = FaceData_Memory + i * (num_vertices * 3);

Don’t forget to delete FaceData_Memory and FaceData when you don’t need them anymore.

PS: I think that anyone using dynamic multidimensional arrays should always use this method to allocate a contiguous memory block.
Plus it’s faster.

Where are some good tutorials on vertex arrays. I am able to draw something but it doesnt look right. I dont know what I am doing wrong. If anyone has any experience with 2d dynamic arrays and vertex arrys in opengl some help would be appreciated. Thanks.

Yeah, I was talking about allocating 2d arrays using Bob’s example. GPSnoopy’s method would work nicely, though. I still tend to just use a single dimension array and calculate the indices as I need them, them. You can also just use a struct with x,y,z coords (and anything else you might want like color, normal, texcoords, etc.) and then just create an array of that.

and the struct will work for vertex arrays?

Yes, structs work nicely. There are some cases with structs where you can get some padding between members due to pack alignment, but generally you are ok if you stick with variables that have a length of 4 bytes like floats.

Ok thanks that helps