delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/23/07:45:15

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: allocated memory size
Date: 23 May 2000 11:30:53 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 41
Distribution: world
Message-ID: <8gdq5d$j9h$1@nets3.rz.RWTH-Aachen.DE>
References: <Pine DOT LNX DOT 4 DOT 10 DOT 10005200546470 DOT 2249-100000 AT roadrunner DOT grendel DOT net> <8gbas1$c2b$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE> <ptsjis4i1m77ahpaohvoe0v274533mfj4n AT 4ax DOT com>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 959081453 19761 137.226.32.75 (23 May 2000 11:30:53 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 23 May 2000 11:30:53 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019