home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!news.claremont.edu!bridge2!sgiblab!swrinde!gatech!concert!sas!mozart.unx.sas.com!sasghm
- From: sasghm@theseus.unx.sas.com (Gary Merrill)
- Newsgroups: comp.lang.c
- Subject: Re: Is this ANSI?
- Message-ID: <By0vu2.7AF@unx.sas.com>
- Date: 20 Nov 92 16:15:37 GMT
- References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 64
- Originator: sasghm@theseus.unx.sas.com
- Nntp-Posting-Host: theseus.unx.sas.com
-
-
- In article <24238@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
- |> In article <9eRguB1w165w@quest.UUCP> srhqla!quest!kdq writes:
- |>
- |> > Here is (a simplified version of) the struct that the compiler
- |> > calls an error:
- |>
- |> > struct FM
- |> > {
- |> > short data_len;
- |> > char data[]; /* This line is the kicker */
- |> > };
- |>
- |> > As I recall, ANSI says this is legal, with sizeof( struct FM) is 2
- |> > bytes, and offsetof( struct FM, data) is 2.
- |>
- |> I don't think you recall correctly. If nothing else, there is no
- |> requirement that sizeof(short) <= 2. Moreover, since every object,
- |> including members of structures, must have nonzero size, the line
- |> in question would have to allocate at least one element. I would
- |> therefore expect that if I hunted through my copy of the Standard,
- |> I would find that this fragment is indeed illegal.
-
- It sure is illegal. Some compilers (our mainframe compiler, for example)
- allow this as an extension. It's not a good idea to code this even if
- it is permitted.
-
- |> I'm not going to bother to hunt, though, because I think your question
- |> conceals a more important question that I can answer much more easily.
- |> The point is to ask why you're trying to do this in the first place.
- |>
- |> That is, why bother with what looks like an array with no elements?
- |> What use is it? The only answer I can think of is that you're intending
- |> to allocate `extra' memory and then subscript off the end of the array --
- |> and the standard definitely does not allow that technique, period.
-
- I think "does not allow that technique" is a little strong. What you
- *can* do is
-
- struct FM
- {
- short data_len;
- char data[1];
- };
-
- struct FM *fmptr;
-
- fmptr = malloc( sizeof(struct FM) + ??? );
-
- where the ??? is the number of bytes of your data minus 1. Some may
- regard this as "impure", but it is not forbidden by the standard and
- is eminently portable. Why would you want to do this? Well, it
- can really reduce the number of malloc() calls and hence the
- function call overhead in general. I have achieved quite measurable
- performance improvements on some systems with this technique.
-
- It's not so hot if you ever want to *change* the size of the data
- array, free it, etc. If you are going to do ugly stuff, you need
- to think about it carefully and weigh the benefits against possible
- future costs.
- --
- Gary H. Merrill [Principal Systems Developer, C Compiler Development]
- SAS Institute Inc. / SAS Campus Dr. / Cary, NC 27513 / (919) 677-8000
- sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
-