#include
void* malloc2d( int w, int h, int size )
{
int j;
void **a = (void**) malloc( h*sizeof(void*) + w*h*size );
for( j=0; j
return a;
}
void* malloc3d( int w, int h, int d, int size )
{
int j, k;
void ***a = (void***) malloc( d*sizeof(void**) + h*d*sizeof(void*) + w*h*d*size );
for( k=0; k
for( k=0; k
return a;
}
class Array2D
{
public:
int w, h;
int **m;
Array2D( int width, int height )
{
w = width;
h = height;
m = (int**)malloc2d(w,h,sizeof(int));
}
~Array2D(){free(m);}
};
class Array3D
{
public:
int w, h, d;
int ***m;
Array3D( int width, int height, int depth )
{
w = width;
h = height;
d = depth;
m = (int***)malloc3d(w,h,d,sizeof(int));
}
~Array3D(){free(m);}
};
void main()
{
Array3D Array3DExample(3, 4, 6);
Array2D Array2DExample(5, 2);
int counter = 0;
for(int j=0; j
for(int i=0; i
Array2DExample.m[j][i] = counter++;
for(int j=0; j
{
for(int i=0; i
{
printf("Index, X:[%d], Y:[%d]= %d\n", i, j, Array2DExample.m[j][i] );
}
}
counter = 0;
for(int k=0; k
for(int j=0; j
for(int i=0; i
Array3DExample.m[k][j][i] = counter++;
for(int k=0; k
{
for(int j=0; j
{
for(int i=0; i
{
printf("Index, X:[%d], Y:[%d], Z:[%d] = %d\n", i, j, k, Array3DExample.m[k][j][i] );
}
}
}
}
沒有留言:
張貼留言