home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16885 < prev    next >
Encoding:
Internet Message Format  |  1992-11-20  |  3.0 KB

  1. Path: sparky!uunet!news.claremont.edu!bridge2!sgiblab!swrinde!gatech!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: <By0vu2.7AF@unx.sas.com>
  6. Date: 20 Nov 92 16:15:37 GMT
  7. References: <9eRguB1w165w@quest.UUCP> <24238@alice.att.com>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 64
  11. Originator: sasghm@theseus.unx.sas.com
  12. Nntp-Posting-Host: theseus.unx.sas.com
  13.  
  14.  
  15. In article <24238@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
  16. |> In article <9eRguB1w165w@quest.UUCP> srhqla!quest!kdq writes:
  17. |> 
  18. |> >     Here is (a simplified version of) the struct that the compiler
  19. |> > calls an error:
  20. |> 
  21. |> > struct FM
  22. |> > {
  23. |> >     short    data_len;
  24. |> >     char    data[];        /*  This line is the kicker  */
  25. |> > };
  26. |> 
  27. |> >     As I recall, ANSI says this is legal, with sizeof( struct FM) is 2 
  28. |> > bytes, and offsetof( struct FM, data) is 2.
  29. |> 
  30. |> I don't think you recall correctly.  If nothing else, there is no
  31. |> requirement that sizeof(short) <= 2.  Moreover, since every object,
  32. |> including members of structures, must have nonzero size, the line
  33. |> in question would have to allocate at least one element.  I would
  34. |> therefore expect that if I hunted through my copy of the Standard,
  35. |> I would find that this fragment is indeed illegal.
  36.  
  37. It sure is illegal.  Some compilers (our mainframe compiler, for example)
  38. allow this as an extension.  It's not a good idea to code this even if
  39. it is permitted.
  40.  
  41. |> I'm not going to bother to hunt, though, because I think your question
  42. |> conceals a more important question that I can answer much more easily.
  43. |> The point is to ask why you're trying to do this in the first place.
  44. |> 
  45. |> That is, why bother with what looks like an array with no elements?
  46. |> What use is it?  The only answer I can think of is that you're intending
  47. |> to allocate `extra' memory and then subscript off the end of the array --
  48. |> and the standard definitely does not allow that technique, period.
  49.  
  50. I think "does not allow that technique" is a little strong.  What you
  51. *can* do is
  52.  
  53.     struct FM
  54.     {
  55.        short data_len;
  56.        char  data[1];
  57.         };
  58.  
  59.     struct FM *fmptr;
  60.  
  61.     fmptr = malloc( sizeof(struct FM) + ??? );
  62.  
  63. where the ??? is the number of bytes of your data minus 1.  Some may
  64. regard this as "impure", but it is not forbidden by the standard and
  65. is eminently portable.  Why would you want to do this?  Well, it
  66. can really reduce the number of malloc() calls and hence the
  67. function call overhead in general.  I have achieved quite measurable
  68. performance improvements on some systems with this technique.
  69.  
  70. It's not so hot if you ever want to *change* the size of the data
  71. array, free it, etc.  If you are going to do ugly stuff, you need
  72. to think about it carefully and weigh the benefits against possible
  73. future costs.
  74. -- 
  75. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  76. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  77. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  78.