C/C++ >> 2D array, malloc, pointers etc.
Posted by beezo on 13:58:00 10-15-2003
I have a function that fills a 2D array with verticies but i want the 2D array to be declared outside the function, where the size is unknown. so the function only manipulates the array.

I don't know what parameter to pass in, or how to declare the function. Can someone have a read, and tell me the right (better) way to do it?

what i've got so far:
Code:

typedef struct {
float x, y, z;
} VERTEX;

VERTEX **points;

void build_shape( float fi, float theta ) {
float fi;
float theta;
float delta = PI / scale;

points = (VERTEX**)malloc(sizeof(VERTEX)*(scale*2+1));
/* as in: VERTEX points[scale*2+1][scale*2+1] */

VERTEX v;
v.x = 0; /* initialisation */
v.y = 0;
v.z = 0;

if( surface == WIREFRAME ) {
/* horizontal lines in the wire-frame rendering */
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
for( theta = 0; theta <= 2*PI; theta += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}

/* vertical lines in the wire-frame rendering */
for( theta = 0.0; theta <= 2*PI; theta += delta ) {
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}
}
}


[ This Message was edited by: cowsarenotevil on 2003-10-16 05:11 ]
Posted by beezo on 14:00:00 10-15-2003
damn, spaces aren't recognised properly, and i'm too lazy to go back and put everywhere. i'm sure you'll still understand.

EDIT: Fixed for you



[ This Message was edited by: cowsarenotevil on 2003-10-16 05:11 ]
Posted by cowsarenotevil on 05:14:00 10-16-2003
I'm not entirely sure what you mean. You seem to want to allocate the array's memory outside of the function, so would it work to make another function to do that? Or am I missing your point entirely (Which I'm very good at doing )
Posted by C_Rdd on 05:43:00 10-16-2003
I'd recommed that you just return a pointer to the array that you have created inside the function. Alternatively, though it breaks proceedural programming parameters, Declare a global Vertex pointer, which it seems you have done, and then you don't need to pass it, and you can change and assign it at will inside the function. I don't do C/C++ programming, but from what I can see, shouldn't that work? Or if there is a problem, it could be something else.

EDIT: Fixed evil censoring for you.

[ This Message was edited by: cowsarenotevil on 2003-10-16 05:53 ]
Posted by beezo on 10:42:00 10-16-2003
this is what i now have:

Code:
void build_shape( float fi, float theta )
{
/* the amount of detail in the surface */
float delta = PI / denom; /* scale increased, denominator is too */

float SIZE = denom * 2 + 1;
int col;
VERTEX v;

v.x = 0; /* initialisation */
v.y = 0;
v.z = 0;

/* 2D array memory allocation */
points = malloc( SIZE * SIZE * sizeof(VERTEX *) );

/* allocate room for pointers to rows */
points = malloc( SIZE * sizeof(VERTEX *) ); /* X */
if( points == NULL )
{
printf("Failure to allocate memory for 2D arrayn");
exit(0);
}

/* and now we 'point' the pointers */
for( col = 0; col < SIZE; col++)
points[col] = malloc( SIZE * sizeof(VERTEX) ); /* Y */

/* use points[x][y] as normal */

if( surface == WIREFRAME )
{
/* horizontal lines in the wire-frame rendering */
for( fi = 0.0; fi <= 2*PI; fi += delta )
{
for( theta = 0; theta <= 2*PI; theta += delta )
{
calc_vertex( fi, theta, &v );
/* points[(fi/(PI/denom))][(theta/(PI/denom))] = v; */
*(*(points + (fi/(PI/denom))) + (theta/(PI/denom))) = v;
}
}


/* vertical lines in the wire-frame rendering */
for( theta = 0.0; theta <= 2*PI; theta += delta )
{
for( fi = 0.0; fi <= 2*PI; fi += delta )
{
calc_vertex( fi, theta, &v );
/* points[(fi/(PI/denom))][(theta/(PI/denom))] = v; */
*(*(points + (fi/(PI/denom))) + (theta/(PI/denom))) = v;
/* how do I use the array now? is this ok? */
}
}
}

/* At what stage to I free the memory? probably
* outside the function after I use the
* array values right? */
} /* END function */

int row, col;
/* Use array values here somewhere */
for( row = 0; row < SIZE; row++ )
{
for( col = 0; col < SIZE; col++ )
{
/* for lack of a better example! */
printf( "row=%d, col=%d, data=%dn", row, col, points[row][col] );
}
}

/* Is that okay, or do I have to pass the **points array in/out as a parameter? */

/* free the allocated memory */
for ( col = 0 ; col < X ; col++ ) {
free( points[col] );
}
free( points );
}


[ This Message was edited by: beezo on 2003-10-16 10:48 ]

[ This Message was edited by: beezo on 2003-10-16 10:50 ]