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

  1. Xref: sparky comp.std.c:2589 comp.std.c++:1134
  2. Newsgroups: comp.std.c,comp.std.c++
  3. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  4. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  5. Subject: Zero-length structures and pointer comparisons
  6. Message-ID: <9225302.22791@mulga.cs.mu.OZ.AU>
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. Date: Tue, 8 Sep 1992 16:14:16 GMT
  10. Lines: 41
  11.  
  12. First, a question for comp.std.c:
  13. is the following a legal (strictly conforming) program?
  14.  
  15.     struct S { unsigned:0; }
  16.     int main() { return sizeof(S); }
  17.  
  18. Secondly, some comments about C++.
  19. In ANSI C, pointer comparisons between pointers to unrelated objects (ie.
  20. objects that are not both members of the same aggregate) cause undefined
  21. behavior, I believe. For example, the following program
  22.     #include <assert.h>
  23.     int main() {
  24.         int x,y;
  25.         assert(&x != &y);
  26.     }
  27. should cause undefined behavior. [Is this correct?]
  28.  
  29. Now the C++ reference manual (5.9) says basically the same thing, with the
  30. further constraints that comparisons with pointers to static members are
  31. undefined, as are comparisons between pointers to members whose declarations
  32. are separated by an access-class specifier (public/protected/private).
  33.  
  34. Section 5.9 says that "Two pointers to the same object compare equal",
  35. but it does NOT say that "pointers to distinct objects compare unequal" -
  36. instead it says "... Other pointer comparisons are implementation dependent."
  37.  
  38. Given that this is the case, it seems contradictory that repeated calls to
  39. operator new(0) are required to return pointers to "distinct objects".
  40. How could a conforming program possibly detect whether this requirement
  41. was actually met? Surely the following program
  42.     #include <assert.h>
  43.     int main() {
  44.         assert(operator new(0) != operator new(0));
  45.     }
  46. is not strictly conforming, is it?
  47.  
  48. -- 
  49. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  50. This .signature virus is a self-referential statement that is true - but 
  51. you will only be able to consistently believe it if you copy it to your own
  52. .signature file!
  53.