Variable-length messages.

David Keppel pardo at june.cs.washington.edu
Sat Oct 22 12:26:52 AEST 1988


>[ want var-size structures:  struct foo { int siz; char c[1] }; ]

To make the code a little clearer, have a global #define:

	/*
	 * When this is used, it means that the structure it appears
	 * in can be allocated by malloc() as an arbitrary-size
	 * structure.
	 */
	#define VARSIZE 1

then you can do:

	struct foo {
		unsigned size;
		your_type storage[VARSIZE];
	}

Mallocing them is still a trifle wierd:

	thing = malloc (sizeof(unsigned) + n * sizeof(your_type));

because the structure could have holes in it.  (Yecch.)  Slightly
more portable:

	thing = malloc (sizeof(struct foo) - sizeof(unsigned)
			    + (n-1)*sizeof(your_type));

I'm not sure if this is portable.  I appeal to the net gods for
further light.  Anybody?

	;-D on  ( My ignorance knows no bounds-checking )  Pardo
-- 
		    pardo at cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo



More information about the Comp.lang.c mailing list