Re: pointers for c vs c++
On Feb 3, 7:43 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze wrote:
And within the bounds of what each language supports. C
doesn't have an implicite pointer to derived to pointer to
base conversion, for example (for the obvious reason that it
doesn't have derived and base).
Is it so that casting a pointer to a different type in C never
changes the internal value of the pointer, while the same is
not true for C++ (where certain casts in an inheritance
hierarchy can change the actual value of the pointer)?
On machines where all pointer types have the same size and
representation, yes, at least in practice. On a machine where
char* is larger than an int*, casting a char* to an int* can
very definitely change the value of the pointer; the standard
also allows the compiler to enforce alignment (say by anding
with ~3) on a machine where the two otherwise have the same size
and representation (but I've never heard of a compiler which
does this).
Basically, all pointer casts in C are reinterpret_cast (or
const_cast), with more or less the same rules holding as for
reinterpret_cast in C++.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34