TITLE: pointer nuts and bolts (Newsgroups: comp.std.c++, 23 Jun 98) NARAN: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) >(1) >[X] Do all pointers have the same size? This >makes sense because a pointer of any type is >just a memory addresss. CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) No, pointers are not required all to have the same size. It is common to find that pointers all have the same size, but there is no such requirement in the C or C++ language defintion. It is required that void* and char* be compatible, and it is pretty much necessary that a pointer to any kind of struct be the same size as a pointer to any other kind of struct. Don't fall into the trap of "just a memory address". That is mixing levels of abstraction. A pointer in C++ is a programming-language construct that allows you to refer to an object or function. A memory address is an implementation detail. A C++ pointer might be more or less than just a memory address. In addition, a pointer-to-member is in fact a structured type which is completely incompatible with any other kind of pointer or integer. You can't even cast a pointer-to-member to an ordinary pointer or integer type. NARAN: >[X] But what about inheritance where one pointer >can really be two pointers? For example, if >class D derives from class B, the a pointer to >a B can also possibly be a pointer to a D. CLAMAGE: I believe it is necessary for pointers to classes related by inheritance all to have the same size. Since any two classes might be related by inheritance someplace in the program, I don't think an implementation could reasonably wind up with class pointers of different sizes. Pointer to non-class types might well have different sizes. I'm excluding architecture-specific features like "near" and "far" pointers on Intel x86 architectures. They represent an extension of the type system, and are not covered by the C++ standard. NARAN: >[X] The business of casting is just a compile >time check on the correctness of our code. >Correct? CLAMAGE: No. A cast is a way to get around type checking, or to lie to the compiler. Any program using a cast must be presumed to be unsafe, since all safe type conversions (and some unsafe ones) are allowed without writing a cast. An exception is the dynamic_cast, which is either known to be safe at compile time or is checked at run time. NARAN: >(2) Can we assume that sizeof(pointer)=sizeof(int)? CLAMAGE: No. Pointers are bigger than ints on a number of popular architectures, and are bigger than ints on all the 64-bit architectures I am familiar with. It is common for pointers to be the same size as longs, but the C and C++ standards do not require that any integer type be the same size as a pointer. Indeed, on the AS/400 architecture that is the case. AS/400 pointers are 48 bits, and it has no 48-bit integer type as far as I know.