home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: bitwise logical operations on pointers
- Message-ID: <1992Jul24.175209.17306@taumet.com>
- Organization: TauMetric Corporation
- References: <2113@devnull.mpd.tandem.com>
- Date: Fri, 24 Jul 1992 17:52:09 GMT
- Lines: 36
-
- rgp@seiko.mpd.tandem.com (Ramon Pantin) writes:
-
- >Somewhere along the evolution of C this type of expressions where made illegal:
- > char *p = ...;
- > p &= ~1;
-
- >the workaround:
- > p = (char *) ((int)p & ~1);
-
- >makes my code:
- > - harder to write
- > - less portable because it assumes that there is an integral type
- > (if not "int", "long") that has enough precision (bits) so that
- > the casts will be harmless.
-
- You could use 'unsigned long' rather than 'int' to improve the chances of
- the code working.
-
- The workaround is not less portable, since bitwise operations on pointers
- are not portable at all. Any such messing with pointers has to be
- confined to machine-specific modules to have any hope of porting your
- program. If it makes sense to bit-twiddle a pointer, nothing prevents
- a compiler from providing an extension to do so -- the Standard only
- requires the compiler to have a way to diagnose such code as an error.
-
- The reason the C Standard makes such twiddling illegal is that there is
- no way to specify semantics for it (in the context of a programming-
- language Standard). It doesn't make any sense to me to say that a
- construct is completely legal but has no defined semantics.
-
- Those things in the C Standard which have undefined behavior are
- those which are in general infeasible to detect at compile time.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-