home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / cplus / 948 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  3.8 KB

  1. Path: sparky!uunet!cs.utexas.edu!devnull!rgp
  2. From: rgp@mpd.tandem.com (Ramon Pantin)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: bitwise logical operations on pointers
  5. Message-ID: <2120@devnull.mpd.tandem.com>
  6. Date: 25 Jul 92 00:11:36 GMT
  7. References: <2113@devnull.mpd.tandem.com> <1992Jul24.175209.17306@taumet.com>
  8. Sender: news@devnull.mpd.tandem.com
  9. Organization: Tandem Computers, Micro-Products Division
  10. Lines: 84
  11.  
  12. In article <1992Jul24.175209.17306@taumet.com> steve@taumet.com (Steve Clamage) writes:
  13. >rgp@seiko.mpd.tandem.com (Ramon Pantin) writes:
  14. >
  15. >>Somewhere along the evolution of C this type of expressions where made illegal:
  16. >>    char    *p = ...;
  17. >>    p &= ~1;
  18. >
  19. >>the workaround:
  20. >>    p = (char *) ((int)p & ~1);
  21. >
  22. >>makes my code:
  23. >>    - harder to write
  24. >>    - less portable because it assumes that there is an integral type
  25. >>      (if not "int", "long") that has enough precision (bits) so that
  26. >>      the casts will be harmless.
  27. >
  28. >You could use 'unsigned long' rather than 'int' to improve the chances of
  29. >the code working.
  30.  
  31. I already said 'if not "int", "long"' in my previous paragraph.
  32.  
  33. >
  34. >The workaround is not less portable, since bitwise operations on pointers
  35. >are not portable at all.
  36.  
  37. What an argument!  With K&R compilers and off the shelf 32 bit
  38. processors: MIPS, Sparc, 88K, i[34]86 in 32 bit mode, VAX, NS-32X32,
  39. VAX, RS/6000, etc (_long_ etc here) it is perfectly possible to write a
  40. portable Virtual Memory Subsystem where the portable code (i.e. MMU
  41. independent code) uses bitwise logical operations on pointers to
  42. perform the splitting of an address into a page frame number and the
  43. offset within the page.  Now, the same code could be used on a processor
  44. were pointers are 64 bits and both "ints" and "longs" are 32 bits,
  45. for example, with a compiler where some non-portable code would be
  46. broken if "longs" were 64 bits.  Now if I use the "workaround", then
  47. my pointers would get truncated to 32 bits, it is really _less_ portable.
  48. Of course, a workaround based on unions of a pointer and an array of
  49. some integral type could also be used (even more uglier code).
  50.  
  51. > ... Any such messing with pointers has to be
  52. >confined to machine-specific modules to have any hope of porting your
  53. >program.
  54.  
  55. For the class of machines that I'm interested in, my K&R code is portable
  56. among them, no need to call it machine-specific in this case.
  57.  
  58. > ...  If it makes sense to bit-twiddle a pointer, nothing prevents
  59. >a compiler from providing an extension to do so -- the Standard only
  60. >requires the compiler to have a way to diagnose such code as an error.
  61.  
  62. Most ANSI-C compilers that I have tried this on simply say something
  63. like "error: operators of different types".  Most compiler writters
  64. seem to simply try to implement what the standard dictates and nothing
  65. else.
  66.  
  67. >
  68. >The reason the C Standard makes such twiddling illegal is that there is
  69. >no way to specify semantics for it (in the context of a programming-
  70. >language Standard).
  71.  
  72. The semantic would be "the logical bitwise operation is performed
  73. on the raw bits of the pointer storage with the semantics of the
  74. operation being exactly the same as the semantics for the operation
  75. on an unsigned integral type on that machine."
  76.  
  77. >... It doesn't make any sense to me to say that a
  78. >construct is completely legal but has no defined semantics.
  79.  
  80. How strong was the oposition within ANSI-C with respect to keeping
  81. these traditional K&R semantics?  Were these semantics discarded
  82. because of some purist sense of language design or was there some
  83. vendor with bizare pointers strongly opposed to the acceptance
  84. of these semantics?
  85.  
  86. >Those things in the C Standard which have undefined behavior are
  87. >those which are in general infeasible to detect at compile time.
  88. >-- 
  89. >
  90. >Steve Clamage, TauMetric Corp, steve@taumet.com
  91. >Vice Chair, ANSI C++ Committee, X3J16
  92.  
  93. Thanks for your feedback.
  94.  
  95. Ramon Pantin
  96.