Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ukma!gatech!bbn!cc5.bbn.com!keesan From: keesan@cc5.bbn.com.UUCP Newsgroups: comp.lang.c Subject: Re: Arrays of Unknown Length in Structures (and sizeof) Message-ID: <786@cc5.bbn.com.BBN.COM> Date: Wed, 30-Sep-87 11:03:07 EDT Article-I.D.: cc5.786 Posted: Wed Sep 30 11:03:07 1987 Date-Received: Fri, 2-Oct-87 03:30:39 EDT References: <243@mit-prep.ARPA> <1044@ius1.cs.cmu.edu> Reply-To: keesan@bbn.com (Morris M. Keesan) Organization: Bolt Beranek and Newman, Cambridge, MA Lines: 46 In article <1044@ius1.cs.cmu.edu> edw@ius1.cs.cmu.edu (Eddie Wyatt) writes: >In article someone writes: >> struct STRUCT_FOO >> { >> int one; >> int two; >> int open[]; >> }; >> >> foo_p = malloc(sizeof (struct STRUCT_FOO) + (7 * sizeof (int))); >> >> for (i = 0; i < 7; ++i) >> foo_p->open[i] = i * 100; >> >..... >> As far as I'm concerned, if this isn't kosher, it should be! > > Let me give you a reason of why this should not be "kosher". > > I don't think it is required in C that the fields in a structure > be phyically ordered in the same ordering they are declared. > Hence any of the 6 permutations of the fields you describe > are valid for phyical offset orderings (ie open could have > an offset of 0, two could have an offset of 0 and one could > have an offset of 0 + sizeof(int)). This would wreak havoc!! From K&R p. 196, C Reference Manual 8.5 Structure and union declarations: Within a structure, the objects declared have addresses which increase as their declarations are read left-to-right. The reason the above is not kosher is that int open[] declares an array of unknown length, and therefore the enclosing structure is of unknown length and sizeof(struct STRUCT_FOO) is undefined. However, given the above citation from K&R, I think the example would be completely kosher if "int open[]" were replaced with "int open[0]". There's nothing forbidding zero-sized arrays. Even the 4.2BSD cc allows them, while issuing a bogus error message about "illegal zero-sized arrays". The published Draft Standard from X3J11 introduces this prohibition, and I complained of this in my formal comments. (Speaking of formal comments, my form-letter reply says that the committee's response will probably be coming in September 1987. Any update from committee members present? Doug? Courtney?) -- Morris M. Keesan keesan@bbn.com {harvard,decvax,ihnp4,etc.}!bbn!keesan