Followup: Variable-length string at - (nf)

dan at haddock.UUCP dan at haddock.UUCP
Wed Jul 11 13:37:33 AEST 1984


#R:wjh12:-50000:haddock:12400011:000:937
haddock!dan    Jul  9 22:27:00 1984

I've done this a lot too.  Please note that in general, you can't just
take sizeof(struct) - 1 as the size of the fixed-length prefix, since
C structures are generally padded at the end.  Obviously for most 
applications it doesn't matter if you malloc a byte or three more than
you need, but for one application I ran into, I had to get the
length exactly right.  I was reduced to

    struct a {
	...
	char bytes[1];
    };

#define PREFIXSIZE (  (int) & ((struct a *)NULL)->bytes[0] )

which worked, but is probably not very portable... it does depend on
the compiler not putting padding between the fixed-length prefix and
the char, which is a moderately safe bet.  Since it was kernel code,
I didn't care TOO much about portability.

Of course, I could have defined the ellipsis above as a structure
in its own right--except that the last thing in the ellipsis was 
another odd-sized char array!  Too bad C doesn't have "packed"...



More information about the Comp.lang.c mailing list