home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2594 < prev    next >
Encoding:
Text File  |  1992-09-09  |  3.0 KB  |  70 lines

  1. Path: sparky!uunet!mcsun!uknet!acorn!ixi!clive
  2. From: clive@x.co.uk (Clive Feather)
  3. Newsgroups: comp.std.c
  4. Subject: Re: struct hack
  5. Message-ID: <1992Sep09.112101.1139@x.co.uk>
  6. Date: 9 Sep 92 11:21:01 GMT
  7. References: <9209080014.AA03467@enet-gw.pa.dec.com>
  8. Organization: IXI Limited, Cambridge, UK
  9. Lines: 59
  10.  
  11. In article <9209080014.AA03467@enet-gw.pa.dec.com> diamond@jit081.enet.dec.com (08-Sep-1992 0914) writes:
  12. >In article <1992Sep07.104932.20060@x.co.uk> clive@x.co.uk (Clive Feather) writes:
  13. >>In article <9209040741.AA16120@enet-gw.pa.dec.com> diamond@jit081.enet.dec.com(04-Sep-1992 1639) writes:
  14. >>> You can't go past the end of an array object.  But if malloc() or some other
  15. >>> variable has defined the end of the actual array object, then the + operator
  16. >>> can get you that far, regardless of the declared type that some other array
  17. >>> variable had before getting flattened to a pointer.
  18. >> But there is an intepretation that says that, given
  19. >>    int a [5][5];
  20. >> the access "a [1][6]" is illegal, because it goes past the bounds of the
  21. >> array "a [1]". In other words, the declared type of the array does
  22. >> restrict what can happen to a pointer derived from it.
  23. > Is that an actual interpretation ruling or was that only someone's posted
  24. > opinion on Usenet?
  25.  
  26. RFI 17, item 16.
  27.  
  28. | For an array of arrays, the permitted pointer arithmetic in Standard
  29. | ##3.3.6 Semantics (page 48, lines 12-40) is to be understood by
  30. | interpreting the use of the word "object" as denoting the specific
  31. | object determined directly by the pointer's type and value, *not* other
  32. | objects related to that one by contiguity. For example, the following
  33. | code has undefined behaviour:
  34. |     int a [4][5];
  35. |     a [1][7] = 0;  /* undefined */
  36. | Some conforming implementations may choose to diagnose an "array bounds
  37. | violation", while others may choose to interpret such attempted accesses
  38. | successfully with the "obvious" extended semantics.
  39.  
  40. This RFT applies to slices of arrays, but, in *my* opinion, it is
  41. extendable to this case:
  42.  
  43.     struct fred { int i; char s [1]; } *f;
  44.     char *s;
  45.  
  46.     /* ... */
  47.     f = malloc (sizeof f + strlen (ss));
  48.     if (f != NULL)
  49.         strcpy (f->s, ss);
  50.  
  51. The pointer f->s points to an object with type "char [1]", and so, if
  52. strlen (ss) > 0, the access to (f->s)[1] required by strlen is undefined
  53. according to this RFI, even though the array has already decayed to a
  54. pointer.
  55.  
  56. > I am also curious as to the "officially" correct thing to do when an
  57. > interpretation ruling contradicts the standard (whether correcting an
  58. > error that everyone agrees was an error, or otherwise).
  59.  
  60. You submit a new RFI asking the same question but with additional
  61. supporting material which contradicts the original ruling.
  62.  
  63. Do you have an example ?
  64.  
  65. -- 
  66. Clive D.W. Feather     | IXI Limited         | If you lie to the compiler,
  67. clive@x.co.uk          | 62-74 Burleigh St.  | it will get its revenge.
  68. Phone: +44 223 462 131 | Cambridge   CB1 1OJ |   - Henry Spencer
  69. Fax:   +44 223 462 132 | United Kingdom      |
  70.