Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!ucbvax!jade!eris!mwm From: mwm@eris.UUCP Newsgroups: comp.lang.c Subject: Re: foo.text[0] Was: Auto variable with sizeof == 0 Message-ID: <2572@jade.BERKELEY.EDU> Date: Thu, 19-Feb-87 18:08:57 EST Article-I.D.: jade.2572 Posted: Thu Feb 19 18:08:57 1987 Date-Received: Sat, 21-Feb-87 05:53:54 EST References: <4114@brl-adm.ARPA> <4053@utcsri.UUCP> <159@batcomputer.tn.cornell.edu> <626@vu-vlsi.UUCP> <4157@utcsri.UUCP> Sender: usenet@jade.BERKELEY.EDU Reply-To: mwm@eris.BERKELEY.EDU (Mike (No one lives forever.) Meyer) Organization: Missionaria Phonibalonica Lines: 50 On the subject of dealing with structure with variable sized text arrays, <4157@utcsri.UUCP> greg@utcsri.UUCP (Gregory Smith) writes: >The problem is that the struct will be padded out after the one-byte >'text' array to meet alignment requirements for the pointer fields. >'sizeof(LINE)' includes this padding. This can be fixed by placing a >substruct around everything but the 'text[1]' declaration, and taking >the size of that struct instead of sizeof(LINE)-1. Or, declare a dummy >struct with the same declarations just to get its size (and *comment >heavily* or someone will change one and not the other). The use of a >dummy struct instead of a substruct would allow you to leave the struct >references unchanged. Both of these methods may fail if 'text' is >an array of things other than chars - i.e. if padding is required >to align 'text' on a more strict boundary than that required by any >previous field in the struct. Nobody said it was easy :-). I like this. Especially if you get the C preprocessor to help you like so: #define FUNKY_HEADER struct LINE *nextline;\ struct LINE *prevline;\ short size;\ short used; /* Note semicolon! */ struct funky_dummmy_for_size { FUNKY_HEADER } ; struct real_thing { FUNKY_HEADER char text[1]; } ; #define get_new_real_thing(len) \ ((struct real_thing *) malloc(sizeof(funky_dummy_for_size) + len)) ; This takes care of the problem of keeping the two versions in synch. I'm going to look at it for the next version of mg. Of course, someone will probably point at a good reason why this is broken before then, anyway. :-)