home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!rutgers!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: <By6HMs.I4r@unx.sas.com>
- Date: 23 Nov 92 16:54:28 GMT
- References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com> <By0vu2.7AF@unx.sas.com> <24262@alice.att.com>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 69
- Originator: sasghm@theseus.unx.sas.com
- Nntp-Posting-Host: theseus.unx.sas.com
-
-
- In article <24262@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
- |> In article <By0vu2.7AF@unx.sas.com> sasghm@theseus.unx.sas.com (Gary Merrill) writes:
- |>
- |> > 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.
- |>
- |> Oh, really? Can you give me concrete evidence that the C standard allows it?
- |>
- |> More specifically, after executing the code above, can you give me
- |> evidence that referrint to fmptr->data[i] is allowed when i>1?
- |> --
- |> --Andrew Koenig
- |> ark@europa.att.com
-
- Seems to follow quite directly from 3.3.2.1. I don't see any
- problem here at all.
-
- One may want to argue that any reference beyond the end of the
- array is not ensured of being a valid pointer (3.3.6), and I
- concede the obvious fact that if a struct FM were *defined*,
- then such a reference is "undefined". However, in the example
- given, the space is allocated via a malloc() call and hence
- must conform to the requirements for such allocations (4.10.3).
-
- I guess the question then reduces to: Could an ANSI-conforming
- compiler allocate memory in such a way that the given example
- would not work? I may have to admit that a compiler *could*
- do this -- but it would appear to take a special effort of
- perversity to accomplish it. The memory allocated will be contiguous.
- Then &fmptr->data[0] will point into this contiguous memory.
- Now, how to arrange it so that &fmptr->data[2] does *not* point
- into this contiguous memory ... I suppose it can be done.
-
- This seems especially perverse since one could easily address
- the desired memory by a pointer initialized to &data[0]. Thus,
- for example,
-
- char *p;
-
- p = &fmptr->data[0];
-
- and
-
- p[1024]
-
- (or whatever) is perfectly okay. But data[1024] isn't. It
- isn't as though the memory isn't there, or that we can't
- reference it. Makes you wonder.
-
-
- --
- 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
-