The C standard allows pointers to different types to have different sizes, e.g. sizeof(char*) != sizeof(int*)
is permitted. It does, however, require that if a pointer is converted to a void*
and then converted back to its original type, it must compare as equal to its original value. Therefore, it follows logically that sizeof(void*) >= sizeof(T*)
for all types T
, correct?
On most common platforms in use today (x86, PPC, ARM, and 64-bit variants, etc.), the size of all pointers equals the native register size (4 or 8 bytes), regardless of the pointed-to type. Are there any esoteric or embedded platforms where pointers to different types might have different sizes? I'm specifically asking about data pointers, although I'd also be interested to know if there are platforms where function pointers have unusual sizes.
I'm definitely not asking about C++'s pointer-to-members and pointer-to-member-functions. Those take on unusual sizes on common platforms, and can even vary within one platform, depending on the properties of the pointer-to class (non-polymorphic, single inheritance, multiple inheritance, virtual inheritance, or incomplete type).
struct X *
pointers is the same. The reason is that you can forward declare a struct, e.g.struct X;
and then put a pointer to it in another struct,struct Y { struct X *x; };
and the compiler can still know how much spacestruct Y
requires, without knowing anything aboutstruct X
. – Yukichar * p_dummy
is a pointer of typechar
not a pointer to typechar
– Elwinavoid
pointers). The machine was (16-bit) word addressed. Thechar *
address for a memory location was different from the 'anything_bigger *` address for the same memory location. AFAICR, the pointers were all the same size (32 bits), but the bit patterns were different. This is from the days when going from 1 MiB to 2 MiB of main memory was a massive improvement — the fact that the bit patterns in the most significant bits were different didn't matter. – Donar