Mail Archives: djgpp/2000/05/23/07:45:15
Damian Yerrick <Bullcr_pd_yerrick AT hotmail DOT comremovebullcr_p> wrote:
> // NOT ANSI C!!!
> // This is GNU C and perhaps C99. It uses zero-size arrays.
> typedef struct vector_int
> {
> int _sizeInMem; // the current size of the array in core
> int _numElems; // up to which place the array is currently filled
> int v[0]; // the data
> } vector_int;
There's not really any need to leave the realm of ANSI C for
dynamically sized arrays. You can make 'v' a pointer to the array
element type. Or, to show you how I actually did it, in gnuplot:
typedef struct dynarray {
long size; /* alloced size of the array */
long end; /* index of first unused entry */
long increment; /* amount to increment size by, on realloc */
size_t entry_size; /* size of the entries in this array */
void *v; /* the vector itself */
} dynarray;
You have to cast that void * to something else, before you use it. That
can conveniently be done by a macro, i.e.:
dynarray polygons;
#define plist ((polygon *) polygons.v)
init_dynarray(&polygons, sizeof(polygon), 100, 100);
/* access to elements of the list: */
init_polygon(plist + i); /* or equivalently &(plist[i]) */
plist[i].normal.x = 0.5;
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -