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

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Pointer comparisons
  5. Message-ID: <9235623.26886@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1992Dec9.191606.5665@lucid.com> <BzCG7K.2sG@frumious.uucp> <1992Dec16.202711.22367@bcrka451.bnr.ca> <BzDs2x.wA@frumious.uucp>
  9. Date: Mon, 21 Dec 1992 12:39:25 GMT
  10. Lines: 92
  11.  
  12. pat@frumious.uucp (Patrick Smith) writes:
  13.  
  14. >"Pointers ... may be compared".  Does this mean any pair of pointers
  15. >may be compared, or some pairs may be compared?  It certainly suggests
  16. >"any pair" to me, but some might interpret it as "some pairs".
  17.  
  18. I would interpret it to mean that any pair of pointers may be compared.
  19.  
  20. >"the result depends on the relative positions ... in the address
  21. >space."  What does this mean?
  22.  
  23. I think that this is intended to convey a general idea of the intent of the
  24. standard-writers. I don't think it has any direct implications with
  25. regard to the behaviour of an implementation. Perhaps it would be
  26. more suitable as part of the commentary rather than as part of the
  27. standard itself.
  28.  
  29. >"If two pointers point to elements of the same array ...".
  30. >What if they point to members of elements?
  31. >
  32. >   struct X { int a; int b; };
  33. >   X x[2];
  34. >
  35. >Must it be true that &x[0].a < &x[1].a?  What about &x[0].b < &x[1].a?
  36.  
  37. Not according to the ARM as it stands. There might be a reasonable
  38. case for extending the guarantee to these additional cases.
  39.  
  40. >Let me try to be constructive for a change.  Would something
  41. >similar to the following be reasonable?  (It would clearly need
  42. >much more careful wording and attention to details.)
  43. >
  44. >1) Any two pointers to the same type may be compared, with any of
  45. >   the relational operators.
  46. >
  47. >2) In general, the results of the comparisons need not have anything
  48. >   to do with the objects to which the pointers point.  For example,
  49. >   it is possible that p and q point to the same object, but p != q.
  50. >   (But see below.)
  51.  
  52. I don't think that this is reasonable. It is necessary to be able to
  53. detect when two pointers point to the same object in order to
  54. write correct and efficient assignment operators in many cases.
  55.  
  56. >3) The relational operators define a total ordering on the set of
  57. >   pointers of each type.  (All the normal rules, including
  58. >   transitivity.)
  59. >
  60. >4) If two pointers p and q (of the same type) are obtained through
  61. >   sequences of "normal" operations, then
  62. >
  63. >      p == q   <==>   p and q point to the same object
  64. >
  65. >      [list of other reasonable properties of pointer comparisons]
  66. >
  67. >For rule (4), "normal" operations would include things such as
  68. >the built-in & operator, adding integers to pointers to array
  69. >elements, normal casts (eg. derived class pointer to base class
  70. >pointer), and calls to standard library functions.  It would
  71. >specifically exclude casts whose meaning is not defined by the
  72. >standard (eg. casting an integer to a pointer) and calling an
  73. >extern "C" function.  (Actually, one could develop a notion of a
  74. >C function which only does "normal" operations and permit
  75. >calling such a function.  (Is there already a concept of strictly
  76. >conforming C function, as opposed to program?))
  77.  
  78. There is no need for these new notions of "normal" operations or
  79. "strictly conforming functions". The "normal" operations should
  80. just be all operations whose results are defined by the standard.
  81. (See my earlier responses to John Skaller's posts.)
  82.  
  83. >On systems where a pointer can be treated as an integer address,
  84. >this should pose no problems.
  85. >
  86. >On systems where a pointer is composed of a segment identifier
  87. >and an offset, the compiler might arrange that the "normal"
  88. >operations would never change the segment identifier.
  89. >Comparisons could then be done by just comparing the
  90. >segment identifier and offset.  If two pointers to the same object
  91. >have different segment identifiers, then the programmer must have
  92. >applied some un"normal" operation, so it's OK if the pointers
  93. >compare unequal.
  94.  
  95. This is exactly what C compilers for DOS systems currently do.
  96. It should be straightforward to apply the same approach for the
  97. 386 memory models where pointers are 48 bits.
  98.  
  99. -- 
  100. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  101. This .signature virus is a self-referential statement that is true - but 
  102. you will only be able to consistently believe it if you copy it to your own
  103. .signature file!
  104.