I am reading this post which is related to char
and byte
, and come across the following words:
An
int*
could still be implemented as a single hardware pointer, since C++ allowssizeof(char*) != sizeof(int*)
.
How to understand 'C++ allows sizeof(char*) != sizeof(int*)
'?
There are (or were) machines which could only address entire "words", where a word was large enough to hold several characters. For example, the PDP-6/10 had a word-size of 36-bits. On such a machine, you might implement 9-bit bytes and represent a byte pointer as the combination of a word pointer and a bit-index within the word. A naïve implementation would require two words for such a pointer, even though a integer pointer would be just a word pointer, occupying a single word.
(The real PDP-6/10 allowed for smaller character sizes -- 6- and 7-bit codings were common, depending on use case -- and since a pointer could not occupy a whole word, it was possible to make a character pointer including bit offset and word address fit inside a single word. But a similar architecture these days would not have the draconian restriction on address space, so that wouldn't work anymore.)
In short, the standard doesn't guarantee it, the result is implementation-defined.
From the standard about sizeof
($5.3.3/1 Sizeof [expr.sizeof])
The sizeof operator yields the number of bytes in the object representation of its operand.
and pointer is compound type ($3.9.2/1.3 Compound types [basic.compound])
pointers to void or objects or functions (including static members of classes) of a given type, 8.3.1;
and ($3.9.2/3 Compound types [basic.compound])
The value representation of pointer types is implementation-defined.
even though ($3.9.2/3 Compound types [basic.compound])
Pointers to layout-compatible types shall have the same value representation and alignment requirements (3.11).
but char
and int
don't need to have the same value representation. The starndard only says ($3.9.1/2 Fundamental types [basic.fundamental])
There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list.
and ($3.9.1/3 Fundamental types [basic.fundamental]) etc.
each signed integer type has the same object representation as its corresponding unsigned integer type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With