Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: Zero Length Arrays Allowed in C Standard? Message-ID: <1887@crdos1.crd.ge.COM> Date: 7 Dec 89 16:43:33 GMT References: <2298@jato.Jpl.Nasa.Gov> <11715@smoke.BRL.MIL> <480@codonics.COM> <70188@psuecl.bitnet> <12468@ulysses.homer.nj.att.com> Organization: GE Corp R&D Center, Schenectady NY Lines: 26 Reply-exos:@crdgw1:To: davidsen@crdos1.crd.ge.com (bill davidsen) In article <12468@ulysses.homer.nj.att.com> gsf@ulysses.homer.nj.att.com (Glenn Fowler[drew]) writes: | why not make the last element huge rather than small and then at malloc time | decrease the sizeof rather than increase: | | [ example ] | | or is this just as sleazy as the undersized array example? It has one advantage over the use of size zero or one: if you have a compiler which checks subscripts (I haven't seen one, but Sabre might) this will allow subscripts up to the max range. The advantage of the zero size array is that then the size of the malloc is (sizeof(struct whatever) + datasize), while in the case of the big declaration its (sizeof(struct whatever) - (maxsize - datasize)). The former is easier to write and understand, although I'd bury the whole thing in a macro called with just the size. #define getstructx(size) \ (struct whatever *)malloc(sizeof(struct whatever)+size) -- bill davidsen (davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen) "The world is filled with fools. They blindly follow their so-called 'reason' in the face of the church and common sense. Any fool can see that the world is flat!" - anon