Advertisement

Dynamic multi-dimensional arrays

Started by April 05, 2001 07:08 PM
2 comments, last by MadCow[an] 23 years, 10 months ago
Is there an easy way to create a dynamically-allocated multi-dimensional array? The following is the way I am creating it at the moment, but there''s surely a faster (and more readable) method! Here I''m creating a vertices[9][3] array. For example, it could hold nine x,y,z vertices... int number = 9; float** vertices; vertices = (float**)malloc(sizeof(float*)*number); for( int i = 0 ; i < number ; ++i) { vertices = (float*)malloc(sizeof(float)*3); } Thanks, MadCow(an)
You could create a single dimensional array and cast it.
Advertisement
Depending on your data type you could use a valarray.

Or a mulitdimensional vector.
Are you using just C? If you''re using C++ you could do:

float (*p)[3] = new float[9][3];

Not sure how you could do it in C, sorry.

Still, in C or C++, I''d use a struct instead.

struct Vertex
{
float x, y, z;
};

Vertex *vertexarray = new Vertex[9];

Jesse Chounard

This topic is closed to new replies.

Advertisement