TITLE: pointer tinkering (Newsgroups: comp.std.c++, 11 Jan 99) PAYNE: Tom Payne >>> Are there conforming ways of hiding and restoring >>> a pointer, such that the restored pointer is dereferencable? BONNARD: Valentin Bonnard >> A pointer is a POD, so it's just a bag od bits and you can play >> with its bits as much as you want. >> >> This includes xor-ing, writing to a file, encription... > >> This is _clearly_ legal, ... CLAMAGE: Steve Clamage > Well, not really. > > You cannot perform bitwise operations on a pointer. You must > first convert the pointer to an integer type. The standard > says that if a suitable integer type exists, you can convert the > pointer value to that type and back to the original type > and the result compares equal to the original. > > The standard does not promise that the integer type exists, > although such a type usually exists. (An exception would be > an AS400 platform with 32-bit longs and 48-bit pointers, > and no "long long" type.) > > If you can perform the conversion to an integer type, the > standard makes no promises about what happens if you modify > the bits and try to store the result back into a pointer type. > Some platforms have hardware checks on pointers, and trying to > store an invalid pointer results in a program trap. Such an > implementation conforms to the standard. > > On many popular platforms you can swizzle pointers without any > problem, but (particularly in comp.std.c++) we have to be careful > about calling the operation valid or portable.