home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17040 < prev    next >
Encoding:
Text File  |  1992-11-23  |  2.9 KB  |  83 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!rutgers!concert!sas!mozart.unx.sas.com!sasghm
  2. From: sasghm@theseus.unx.sas.com (Gary Merrill)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is this ANSI?
  5. Message-ID: <By6HMs.I4r@unx.sas.com>
  6. Date: 23 Nov 92 16:54:28 GMT
  7. References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com> <By0vu2.7AF@unx.sas.com> <24262@alice.att.com>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 69
  11. Originator: sasghm@theseus.unx.sas.com
  12. Nntp-Posting-Host: theseus.unx.sas.com
  13.  
  14.  
  15. In article <24262@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
  16. |> In article <By0vu2.7AF@unx.sas.com> sasghm@theseus.unx.sas.com (Gary Merrill) writes:
  17. |> 
  18. |> > I think "does not allow that technique" is a little strong.  What you
  19. |> > *can* do is
  20. |> 
  21. |>     struct FM
  22. |>     {
  23. |>        short data_len;
  24. |>        char  data[1];
  25. |>         };
  26. |> 
  27. |>     struct FM *fmptr;
  28. |> 
  29. |>     fmptr = malloc( sizeof(struct FM) + ??? );
  30. |> 
  31. |> > where the ??? is the number of bytes of your data minus 1.  Some may
  32. |> > regard this as "impure", but it is not forbidden by the standard and
  33. |> > is eminently portable.
  34. |> 
  35. |> Oh, really?  Can you give me concrete evidence that the C standard allows it?
  36. |> 
  37. |> More specifically, after executing the code above, can you give me
  38. |> evidence that referrint to fmptr->data[i] is allowed when i>1?
  39. |> -- 
  40. |>                 --Andrew Koenig
  41. |>                   ark@europa.att.com
  42.  
  43. Seems to follow quite directly from 3.3.2.1.  I don't see any
  44. problem here at all.
  45.  
  46. One may want to argue that any reference beyond the end of the
  47. array is not ensured of being a valid pointer (3.3.6), and I
  48. concede the obvious fact that if a struct FM were *defined*,
  49. then such a reference is "undefined".  However, in the example
  50. given, the space is allocated via a malloc() call and hence
  51. must conform to the requirements for such allocations (4.10.3).
  52.  
  53. I guess the question then reduces to:  Could an ANSI-conforming
  54. compiler allocate memory in such a way that the given example
  55. would not work?  I may have to admit that a compiler *could*
  56. do this -- but it would appear to take a special effort of
  57. perversity to accomplish it.  The memory allocated will be contiguous.
  58. Then &fmptr->data[0] will point into this contiguous memory.
  59. Now, how to arrange it so that &fmptr->data[2] does *not* point
  60. into this contiguous memory ...  I suppose it can be done.
  61.  
  62. This seems especially perverse since one could easily address
  63. the desired memory by a pointer initialized to &data[0].  Thus,
  64. for example,
  65.  
  66.     char *p;
  67.  
  68.     p = &fmptr->data[0];
  69.  
  70. and
  71.  
  72.     p[1024]
  73.  
  74. (or whatever) is perfectly okay.  But data[1024] isn't.  It
  75. isn't as though the memory isn't there, or that we can't
  76. reference it.  Makes you wonder.
  77.  
  78.  
  79. -- 
  80. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  81. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  82. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  83.