Re: pointer question
Re: pointer question
- Subject: Re: pointer question
- From: Candide Kemmler <email@hidden>
- Date: Tue, 3 Jul 2001 12:55:01 +0200
Hi Stiphane (or should I say "Salut Stiphane" ?)
Thanks for answering.
Anyway doing
roadStore[index]=aRoad;
just crashes my program: here's the whole code (still working on it...):
- ( void ) drawRect: (NSRect) frame
{
NSBezierPath *bpath;
char *buffer, **bufferHandle;
char *layerString;
int rgnTyp, ncoords, i, fow, roadIdx;
NSPoint point, *points;
struct road * roadStore [1024];
struct road *aRoad;
if ( myData != NULL ) {
NSLog ( @"myData is not null !" );
buffer = ( char * ) [ myData bytes ];
bufferHandle = &buffer;
}
bpath = [ NSBezierPath bezierPathWithRect:frame ];
[ [ NSColor blueColor ] set ];
[ bpath fill ];
if ( myData != NULL ) {
NSLog ( @"we are going to really draw something !" );
layerString = readUTF ( bufferHandle );
while ( strcmp ( layerString, "done" ) != 0 ) {
NSLog ( @"next layer is %s", layerString );
if ( strcmp ( layerString, "regions" ) == 0 ) {
NSLog ( @"now we must draw the regions !" );
rgnTyp = readInt ( bufferHandle );
[ [ NSColor greenColor ] set ];
while ( rgnTyp != -1 ) {
[ bpath removeAllPoints ];
ncoords = readInt ( bufferHandle );
point.x = readInt ( bufferHandle );
point.y = readInt ( bufferHandle );
[ bpath moveToPoint:point ];
for ( i=1; i<ncoords; i++ ) {
point.x = readInt ( bufferHandle );
point.y = readInt ( bufferHandle );
[ bpath lineToPoint:point ];
}
[ bpath closePath ];
[ bpath fill ];
rgnTyp = readInt ( bufferHandle );
}
} else if ( strcmp ( layerString, "roads" ) == 0 ) {
NSLog ( @"and now we must draw the roads !" );
fow = readInt ( bufferHandle );
while ( fow != -1 ) {
aRoad = ( struct road * ) malloc ( sizeof ( struct
road ) );
aRoad->formofway = fow;
aRoad->funcrdcl = readInt ( bufferHandle );
aRoad->levbeg = readInt ( bufferHandle );
aRoad->levend = readInt ( bufferHandle );
aRoad->ncoords = readInt ( bufferHandle );
// NSLog ( @"about to allocate memory for %d
coordinates, fow=%d, frc=%d, levbeg=%d, levend=%d", aRoad.ncoords, fow,
aRoad.funcrdcl, aRoad.levbeg, aRoad.levend );
aRoad->points = (NSPoint *) malloc (aRoad->ncoords *
sizeof(NSPoint));
aRoad->points->x=readInt ( bufferHandle );
aRoad->points->y=readInt ( bufferHandle );
for ( i=1; i<aRoad->ncoords; i++ ) {
points += 1;
aRoad->points->x=readInt ( bufferHandle );
aRoad->points->y=readInt ( bufferHandle );
}
roadStore [roadIdx] = aRoad;
roadIdx++;
fow = readInt ( bufferHandle );
}
}
layerString = readUTF ( bufferHandle );
}
free ( layerString );
}
}
Le mardi 3 juillet 2001, ` 11:19, Stiphane Sudre a icrit :
On mardi, juillet 3, 2001, at 10:10 AM, Candide Kemmler wrote:
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 ??
roadStore[index]=aRoad;
Also, a very big concern of mine is: what do I do with all the
dynamically allocated memory ?
As long as your data is needed, you do nothing.
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 ???
If you're not going to use this data anymore, well, yes:
you do :
for(i=0;i< roadStore_number_ofItem;i++)
{
if (roadStore[i]->points!=NULL)
{
free(roadStore[i]->points);
}
free(roadStore[i]);
}
that's all.