home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!bloom-beacon!eru.mt.luth.se!lunic!sunic!aun.uninett.no!ugle.unit.no!sigyn.idt.unit.no!bjornmu
- From: bjornmu@idt.unit.no (Bj|rn P. Munch)
- Newsgroups: comp.lang.c
- Subject: Re: Variable length records
- Message-ID: <1992Jul26.124915.14419@ugle.unit.no>
- Date: 26 Jul 92 12:49:15 GMT
- References: <24137@castle.ed.ac.uk> <rbjmzg-.gurgle@netcom.com> <1992Jul25.091702.15993@klaava.Helsinki.FI>
- Sender: news@ugle.unit.no (NetNews Administrator)
- Organization: Div. of CS & Telematics, Norwegian Institute of Technology
- Lines: 50
-
- In article <1992Jul25.091702.15993@klaava.Helsinki.FI>, wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) writes:
- |> gurgle@netcom.com (Pete Gontier) writes:
- |> >typedef struct
- |> >{
- |> > short count;
- |> > float data[ ];
- |> >}
- |> >foo;
- |> >
- |> >I refer to the second member as an "open array." Its behavior is this:
- |> > o it may appear only as the last member of a structure
- |> > o it does not contribute to the value returned by sizeof(foo)
- |> > o it may be addressed, of course, with an arbitrary index
- |>
- |> Are there any particular reasons to not use
- |>
- |> typedef struct {
- |> short count;
- |> float data[1];
- |> } foo;
- |>
-
- I was about to give the same comment. I use a definition like the
- above myself. The expression for the size you need to "malloc" gets
- more complex (as you need to subtract one), but I don't mind that.
-
- |> (and similar over-allocation and using "too large" indexes) instead?
- |> This version only assumes that there won't be any problems for accessing
- |> elements outside the data array.
- |>
- |> And, except for a minor gain in convenience for allocation and
- |> deallocation, is either of these preferable to
- |>
- |> typedef struct {
- |> short count;
- |> float *data;
- |> } foo;
-
- Even though the syntax for accessing the data will be the same, the
- use of this form will "cost" you one additional pointer dereferencing.
- If you use this structure a lot (as I do), it may make an (ok, rather
- small) difference to the performance.
-
- (It may also cost an extra malloc to set up)
-
- ---
- Bj|rn P. Munch | Dept. of Comp. Science & Telematics,
- Bjoern.P.Munch@idt.unit.no | Norwegian Institute of Technology (NTH),
- PhD Student | N-7034 Trondheim, Norway
- (some filler words here) | Fingerable addr: bjornmu@multe.idt.unit.no
-