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

  1. Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Zero-length structures and pointer comparisons
  5. Message-ID: <4945@holden.lulea.trab.se>
  6. Date: 9 Sep 92 19:26:46 GMT
  7. References: <9225302.22791@mulga.cs.mu.OZ.AU>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 66
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  13. : Secondly, some comments about C++.
  14. : In ANSI C, pointer comparisons between pointers to unrelated objects (ie.
  15. : objects that are not both members of the same aggregate) cause undefined
  16. : behavior,
  17.  
  18. Small clarifications:
  19.  
  20. The ARM is explicit about the relational operators (<,>,<=,>=),
  21. and then goes on to say the equality operators are "exactly analogous".
  22. Anyway, comparison with pointers to members of the same aggregate are only
  23. defined if they belong to the same access zone (simplified).  Also,
  24. pointer comparison within the same array is supported.
  25.  
  26. :I believe. For example, the following program
  27. :     #include <assert.h>
  28. :     int main() {
  29. :         int x,y;
  30. :         assert(&x != &y);
  31. :     }
  32. : should cause undefined behavior. [Is this correct?]
  33.  
  34. I'd say yes.  On a segmented architecture (can you say 'PC'?)
  35. pointer comparison need not use the full 32 bits for pointer comparison,
  36. instead a 16 bit offset may be used (note that this limits arrays to 64 KB).
  37.  
  38. I wonder if the 'PC' segmented architecture will reappear in the
  39. first 64 bit processors (representing pointers as 32 bit offsets within
  40. up to 2^32 segments)?  Does anyone have any info on this?
  41.  
  42. Anyway, the current rules do seem to cause trouble for the common (?)
  43. idiom for operator =:
  44.  
  45. const T& operator = ( const T& t )
  46. {
  47.     if ( &t == this ) return;    // avoid self-assignment
  48.     // ...
  49. }
  50.  
  51. Is the above idiom broken?
  52. Should the current rules be changed (inflicting a small performance penalty
  53. on some machines)?
  54.  
  55. : Given that this is the case, it seems contradictory that repeated calls to
  56. : operator new(0) are required to return pointers to "distinct objects".
  57. : How could a conforming program possibly detect whether this requirement
  58. : was actually met? Surely the following program
  59. :     #include <assert.h>
  60. :     int main() {
  61. :         assert(operator new(0) != operator new(0));
  62. :     }
  63.  
  64. If sizeof(long) >= sizeof(void*), then you should be able to
  65. test like this:
  66.  
  67. assert( (long)p1 != (long)p2 );
  68.  
  69. since a pointer stored in an integer variable must keep all information
  70. needed to restore the pointer (if the integer type is large enough).
  71.  
  72. -- 
  73. --------------------------------------------------------------------------
  74. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  75. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  76. --------------------------------------------------------------------------
  77.