pointer question
pointer question
- Subject: pointer question
- From: Candide Kemmler <email@hidden>
- Date: Tue, 3 Jul 2001 10:10:32 +0200
Hi !
Still on my way to learning C...
I begin to understand the advantage of the "retain" construct...
So, here's my problem:
I have a bunch of polygons to draw, which I get from the network. I
don't know how much of them I'll get in advance. I don't know either how
much vertices each polygon contains (that is, until I receive each
polygon's data). (each polygon stands for a road)
Hence, I've written a struct like so:
struct road {
int levbeg;
int levend;
int formofway;
int funcrdcl;
NSPoint * points;
int ncoords;
};
These are the variables I declare:
char *buffer, **bufferHandle;
int i, fow, roadIdx;
NSPoint point, *points;
struct road * roadStore [1024], *aRoad;
When I receive a polygon's data, I malloc a new pointer to the road data
and assign it to aRoad:
aRoad = ( struct road * ) malloc ( sizeof ( struct
road ) );
then I use the pointer to store my data:
aRoad->formofway = fow;
aRoad->funcrdcl = readInt ( bufferHandle );
aRoad->levbeg = readInt ( bufferHandle );
aRoad->levend = readInt ( bufferHandle );
aRoad->ncoords = readInt ( bufferHandle );
When I reach the ncoords indicator, I malloc a new pointer to the
coordinates data and assign it to points:
points = (NSPoint *) malloc (aRoad->ncoords *
sizeof(NSPoint));
then I use the pointer to store my data:
points->x=readInt ( bufferHandle );
points->y=readInt ( bufferHandle );
for ( i=1; i<aRoad->ncoords; i++ ) {
points += 1;
points->x=readInt ( bufferHandle );
points->y=readInt ( bufferHandle );
}
finally, I assign the pointer to the "points" field of the current road
struct:
aRoad->points = points;
now I need to store aRoad in some kind of array, which is where I
miserably fail:
I tried to do several things to get the aRoad in roadStore (with pointer
arithmetics having declared roadStore a road *, then like here with
arrays, and assigning the roadStore elements with addresses, &aRoad)...
What's the right thing to do ??
Also, a very big concern of mine is: what do I do with all the
dynamically allocated memory ?
Once the loop's finished and I've done what I have to do with the data
(I must sort the polygons and then draw them), do I have to free ()
every aRoad pointer, and in every road, every points pointer ???
I'm getting confused here :-(
Thanks for your help,
Candide