home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / cplus / 1856 < prev    next >
Encoding:
Text File  |  1992-12-21  |  7.6 KB  |  205 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Pointer comparisons
  5. Message-ID: <1992Dec18.202539.22728@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <BzCG7K.2sG@frumious.uucp> <1992Dec16.202711.22367@bcrka451.bnr.ca> <BzDs2x.wA@frumious.uucp>
  10. Date: Fri, 18 Dec 1992 20:25:39 GMT
  11. Lines: 192
  12.  
  13. In article <BzDs2x.wA@frumious.uucp> uunet.ca!frumious!pat writes:
  14. >(I hope nobody minds my changing the subject line to something
  15. >a little more succinct.)
  16. >
  17. >While responding to a previous posting, I looked up what the
  18. >draft C++ standard (the September 17 version) had to say about
  19. >pointer comparisons.  Here's an extract from section 5.9:
  20. >
  21. >   Pointers to objects or functions of the same type (after pointer
  22. >   conversions) may be compared; the result depends on the relative
  23. >   positions of the pointed-to objects or functions in the address
  24. >   space.
  25. >
  26. >   Two pointers to the same object compare equal.  If two pointers
  27. >   point to nonstatic data members of the same object, the pointer
  28. >   to the later declared member compares higher provided the two
  29. >   members [are] not separated by an access-specifier label (11.1)
  30. >   and provided their class is not a union.  If two pointers
  31. >   point to nonstatic members of the same object separated by an
  32. >   access-specifier label (11.1) the result is unspecified.
  33. >   If two pointers point to data members of the same union, they
  34. >   compare equal (after conversion to void*, if necessary).
  35. >   If two pointers point to elements of the same array or one beyond
  36. >   the end of the array, the pointer to the object with the higher
  37. >   subscript compares higher.  Other pointer comparisons are
  38. >   implementation dependent.
  39. >
  40. >Unfortunately, my copy of the C standard isn't at all handy, so
  41. >I can't compare with what it says.
  42. >
  43. >Some of this is quite unclear to me.
  44. >
  45. >
  46. >"Pointers ... may be compared".  Does this mean any pair of pointers
  47. >may be compared, or some pairs may be compared?  It certainly suggests
  48. >"any pair" to me, but some might interpret it as "some pairs".
  49. >
  50.     Seems to many 'any' to me.
  51. >
  52. >"the result depends on the relative positions ... in the address
  53. >space."  What does this mean?  
  54.  
  55.     This means that the committee has committed a grave error.
  56. Too many Unix people. The ARM also commits this error in
  57. assuming the existence of *an* address space. The 386 has
  58. 8K address spaces per process. The idea that relative positioning
  59. of pointers into different segments is meaningful is absurd.
  60.  
  61. >Must one convert the pointers into
  62. >actual addresses and return the result of comparing those addresses?
  63. >Probably not, given that "[o]ther pointer comparisons are
  64. >implementation dependent."  Maybe one is to convert the pointers
  65. >into addresses and apply some (arbitrary) function to those addresses?
  66. >If so, why would one want such a condition?  What happens if there's
  67. >more than one address space?  This clause seems reasonable to me if
  68. >interpreted as "the general intent of pointer comparison is to compare
  69. >addresses", but perhaps it's not suitable as part of the specification
  70. >of the language.
  71. >
  72.     Agreed.
  73. >
  74. >"If two pointers point to elements of the same array ...".
  75. >What if they point to members of elements?
  76. >
  77. >   struct X { int a; int b; };
  78. >   X x[2];
  79. >
  80. >Must it be true that &x[0].a < &x[1].a?  What about &x[0].b < &x[1].a?
  81. >
  82.     I would expect this, but it doesnt say so.
  83. >
  84. >Let me try to be constructive for a change.  Would something
  85. >similar to the following be reasonable?  (It would clearly need
  86. >much more careful wording and attention to details.)
  87. >
  88. >1) Any two pointers to the same type may be compared, with any of
  89. >   the relational operators.
  90.  
  91.     Sure, why not?
  92. >
  93. >2) In general, the results of the comparisons need not have anything
  94. >   to do with the objects to which the pointers point.  For example,
  95. >   it is possible that p and q point to the same object, but p != q.
  96. >   (But see below.)
  97.  
  98.     Yes. It is implementation defined, except in special
  99. circumstances.
  100. >
  101. >3) The relational operators define a total ordering on the set of
  102. >   pointers of each type.  (All the normal rules, including
  103. >   transitivity.)
  104.  
  105.     Accepted as axiom tentatively.
  106.  
  107. >
  108. >4) If two pointers p and q (of the same type) are obtained through
  109. >   sequences of "normal" operations, then
  110. >
  111. >      p == q   <==>   p and q point to the same object
  112. >
  113. >      If p and q point to elements of the same array, or to
  114. >      subobjects of _different_ elements of the same array,
  115. >      then they compare the same way as the indexes of the
  116. >      elements in the array.
  117.  
  118.     Given we defer analysis of "normal" operations this
  119. axiom may contradict axiom 4.
  120.  
  121.     On a linear address machine, 3 & 4 could be made to work,
  122. so such a system can exist. On a segmented machine, 3 & 4 might
  123. be mutually exclusive.
  124. >
  125. >For rule (4), "normal" operations would include things such as
  126. >the built-in & operator, adding integers to pointers to array
  127. >elements, normal casts (eg. derived class pointer to base class
  128. >pointer), 
  129.  
  130.     Dont forget the obvious ones: assignment and initialisation.
  131.  
  132.     Also, some casts might be dubious, espcially casts to/from
  133. void*.
  134.  
  135. >and calls to standard library functions.  
  136.  
  137.     I have some doubts about this.
  138.  
  139. >It would
  140. >specifically exclude casts whose meaning is not defined by the
  141. >standard (eg. casting an integer to a pointer) and calling an
  142. >extern "C" function.  
  143.  
  144.     I think you would have to make ALL calls, even to
  145. C++ functions problematic. Because a called extern C++ function
  146. might do one of the excluded naughty casts.
  147.  
  148.     This doesnt matter though.
  149.  
  150. >On systems where a pointer is composed of a segment identifier
  151. >and an offset, the compiler might arrange that the "normal"
  152. >operations would never change the segment identifier.
  153.  
  154.     This is more complicated than you think.
  155. The 386 'selector' is bits which contain selector access
  156. priviledges, as well as the bits relating to the
  157. actual entry in the descriptor table.
  158.  
  159.     Furthermore, some system might mix 16 and 32 bit
  160. segments (in fact all protected mode operating systems I 
  161. know about allow this) and often have some way of
  162. 'interconverting' between them.
  163.  
  164.     Thus this requirement may be unimplementable.
  165.  
  166. >Comparisons could then be done by just comparing the
  167. >segment identifier and offset.  If two pointers to the same object
  168. >have different segment identifiers, then the programmer must have
  169. >applied some un"normal" operation, so it's OK if the pointers
  170. >compare unequal.
  171.  
  172.     This may or may not be true on the 386  or other machines.
  173. I suspect suitable restrictions on 'normal' might make
  174. this workable.
  175. >
  176. >One might add a library function sameObject(p,q) which would
  177. >indicate whether p and q point to the same object, no matter
  178. >how p and q were obtained.
  179. >
  180.     One might, but it had better not be required.
  181. Because it may be unimplementable on some systems.
  182.  
  183. >
  184. >If we ignore the question of how much existing code this would
  185. >break, is this a reasonable suggestion?
  186.  
  187.     It is reasonable to start from this suggestion,
  188. but you have to look at the fact that axiom 3 and axiom 4
  189. might not be compatible. 
  190. >
  191. >And how much existing code would this break (that isn't already
  192. >broken)?  Note that it doesn't break any code that restricts
  193. >itself to "normal" operations.
  194. >
  195.  
  196.     You dont know that. How do you know that pointers
  197. stored in one segment and those in another, BOTH pointing
  198. to the same object dont have different layouts?
  199.  
  200. -- 
  201. ;----------------------------------------------------------------------
  202.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  203.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  204. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  205.