Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!aimt!breck From: breck@aimt.UUCP (Robert Breckinridge Beatie) Newsgroups: comp.lang.c Subject: Re: Arrays of Unknown Length in Structures (and sizeof) Message-ID: <95@aimt.UUCP> Date: Wed, 30-Sep-87 01:10:25 EDT Article-I.D.: aimt.95 Posted: Wed Sep 30 01:10:25 1987 Date-Received: Sat, 3-Oct-87 07:48:31 EDT References: <243@mit-prep.ARPA> <1044@ius1.cs.cmu.edu> Organization: AIM Technology, Palo Alto, CA Lines: 38 Summary: Possibly another reason that it's not "kosher" In article <1044@ius1.cs.cmu.edu>, edw@ius1.cs.cmu.edu (Eddie Wyatt) 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". > [ Reason deleted for brevity ] Here's another possible reason (corrections welcome) that this isn't "kosher". If the space for the array is allocated in the structure itself, then this makes the size of the structure undefined (I think). What would the compiler do for an array of STRUCT_FOO? What would be the address of bar[10] when bar is an array of STRUCT_FOO? This "feature" is interesting but I don't think it's interesting enough to give up the ability to declare arrays of structures. Also, what would a compiler do if the structure includes more than one field of unknown size? This seems a potentially nightmarish situation. Now since the original article stated that "both [compilers] do what I want" it seems that I may be making a fool of myself again. Have I missed something obvious? What is really going on here? -- Breck Beatie uunet!aimt!breck