Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!mgnetp!ihnp4!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!sdcrdcf!sdcsvax!akgua!mcnc!decvax!cca!ima!haddock!dan From: dan@haddock.UUCP Newsgroups: net.lang.c Subject: Re: Followup: Variable-length string at - (nf) Message-ID: <207@haddock.UUCP> Date: Tue, 10-Jul-84 23:37:33 EDT Article-I.D.: haddock.207 Posted: Tue Jul 10 23:37:33 1984 Date-Received: Wed, 18-Jul-84 03:40:47 EDT Lines: 25 #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"...