From -4118640570568093947
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,9684813612e15361
X-Google-Attributes: gidf78e5,public
From: jcoffin@taeus.com (Jerry Coffin)
Subject: Re: pointers: sizeof(pointer)
Date: 1998/06/23
Message-ID: <MPG.ff8310778f1abb3989b36@news.rmi.net>#1/1
X-Deja-AN: 365198226
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <slrn6oqcgp.lfi.sbnaran@bardeen.ceg.uiuc.edu>
X-Original-Date: Mon, 22 Jun 1998 10:24:22 -0600
Organization: TAEUS
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANY8r9eEDnX0m9pzZAQFoGwF+K+qsi2xWEE3lHnWg+lCZP2umjCYeX2Bi QiHsGjc/qVyLoZjlMg6boaBLfF5krcyB =EtIN
Newsgroups: comp.std.c++


In article <slrn6oqcgp.lfi.sbnaran@bardeen.ceg.uiuc.edu>, 
sbnaran@bardeen.ceg.uiuc.edu says...
> (1)
> 
> [X] Do all pointers have the same size?  This
> makes sense because a pointer of any type is
> just a memory addresss.

No -- pointers to different things may have different sizes.  A 
pointer to void is guaranteed to be able to hold a pointer to any 
other object type, and you can convert it back to a pointer to the 
object type without loss of information though.  (Here I'm using 
"object" in the sense it's used in the C standard, not particularly 
restricted to an instantiation of a class.)  However, this does NOT 
apply to things like pointers to functions, especially pointers to 
member functions, which are typically considerably different from 
other pointers.
 
> [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.

Unless you're dealing with multiple inheritance, this doesn't really 
affect the pointer itself -- it only affects how you interpret what 
the pointer points AT.  You can always convert a pointer to a derived 
type to a pointer to its base type without problems.
 
> [X] The business of casting is just a compile
> time check on the correctness of our code.
> Correct?

Rather the opposite: as far as code correctness goes, a cast 
_prevents_ the compiler from doing testing that it might otherwise do.  
However, a cast can also involve the generation of code -- things like 
casting from a floating point type to an integer type, or doing a 
dynamic_cast are both likely to generate actual code.  In addition, a 
cast may result in constructing an object of the specified type, via a 
call to a constructor, which may involve more or less arbitrary 
amounts of code.
 
> (2) Can we assume that sizeof(pointer)=sizeof(int)?

Not if you're at all interested in portability.  There are quite a few 
environments in which this is not the case right now, and likely to be 
more in the future.

-- 
    Later,
    Jerry.

The Universe is a figment of its own imagination.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



