home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1914 < prev    next >
Encoding:
Text File  |  1992-12-24  |  3.4 KB  |  96 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Pointer comparisons
  5. Message-ID: <1992Dec24.200201.25709@microsoft.com>
  6. Date: 24 Dec 92 20:02:01 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec17.151642.9954@bcrka451.bnr.ca> <1992Dec19.001851.22116@microsoft.com> <1992Dec23.182456.17185@lucid.com>
  9. Lines: 85
  10.  
  11. In article <1992Dec23.182456.17185@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  12. |Jim Adcock is advocating that under some circumstances it
  13. |is ok for a compiler to put two A objects at the same location.
  14.  
  15. NO I AM NOT SO ADVOCATING!  I am saying it is legal for a compiler
  16. to put objects wherever it so likes unless it doesn't explicitly 
  17. and clearly violate a requirement of the ARM.
  18.  
  19. Note that my example DOES NOT put two A objects at the same location.
  20. Rather it puts an A object at the start of a B object such that the
  21. A object has the same starting address as the B object.  My claim is
  22. that from "C" history this is nothing new.  We EXPECT that the first
  23. member of a structure has the same starting address as the structure.
  24. We DO NOT EXPECT a compiler to gratuitously add padding at the
  25. start of a structure.  Any padding in a structure comes between 
  26. members of the structure, or at the end of a structure so that array
  27. elements come out aligned.
  28.  
  29. |His example is
  30. |
  31. ||> class A {};
  32. ||> 
  33. ||> class B : public A
  34. ||> {
  35. ||> public:
  36. ||>     A a;    
  37. ||> };
  38. ||> 
  39. |
  40. |    B b ;
  41. |
  42. |Where he claims it is ok for b.a and (B&)a to compare equal.
  43.  
  44. I assume you make an accidental typo.  On the contrary what I said it is 
  45. okay for &(b.a) and ((A*)&b) to compare equal.  It is okay because ARM
  46. says that unless the ARM states otherwise all pointer comparisons are
  47. *implementation defined*
  48.  
  49. |I don't agree, but I'm wondering exactly what criteria he is applying.
  50.  
  51. The ARM.  Show me a statement in the ARM that clearly contradicts this
  52. behavior.   If no statement clearly contradicts this possible implementation,
  53. the implementation stands, because the ARM *explicitly* states that the
  54. implementation stands unless called out otherwise.
  55.  
  56. |Does anything go for A's.  E.g.
  57. |
  58. |    void f() {
  59. |        A a1, a2 ;  // can &a1==&a2 
  60. |        ... ; 
  61. |        }
  62.  
  63. Of course not!  ARM clearly requires members within a labeled public/private/
  64. protected section to be ordered with later members having "higher" addresses.
  65.  
  66. |Is it only ok for "empty" classes? How about 
  67. |
  68. |    struct D { int i; int j ; } ;
  69. |
  70. |    void f() {
  71. |        D d1, d2 ;
  72. |        ... d1.i ... d2.j ...  // never use d1.j or d2.i 
  73. |            }
  74. |
  75. |Can &d1==&d2.
  76.  
  77. ARM:
  78.  
  79. "Two pointers to the same object compare equal.  If two pointers
  80. point to nonstatic members of the same object, the pointer to the later
  81. declared member compares higher provided the two members not separated
  82. by an *access-specifier* label ($r.11.1) and provided their class is not
  83. a union.  If two pointers point to nonstatic members of the same
  84. object separated by an *access-specifier* label ($r.11.1) the result is
  85. undefined.  If two pointers point to data members of the same union,
  86. they compare equal.  If two pointers point to elements of the same
  87. array or one beyond the end of the array, the pointer to the object
  88. with the higher subscript compares higher.  Other pointer comparisons
  89. are implementation dependent."
  90.  
  91. Is your questioned pointer comparison listed here?  No.
  92.  
  93. Therefore it is EXPLICITLY declared *implementation dependent* by the ARM.
  94.  
  95. Likewise with my first example that you questioned.
  96.