I came to know that
It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes.
But then i came across this which says:
While pointers are all the same size, as they just store a memory address, we have to know what kind of thing they are pointing TO.
Now, i am not sure which one is correct. The second quoted statement looks like from the C notes of Computer science, Florida State University.
Here's what i think:
- Say we have:
int i = 0;
void* ptr = &i;
Now, suppose the standard C allows pointers to have different sizes. Further suppose that on some arbitrary machine/compiler(since it is allowed by the standard), a void*
has size 2 bytes while a int*
has size 4 bytes. Now, i think there is a problem here which is that, the right hand size in the statement shown in the above snippet has an int*
which has size 4 bytes while on the left hand size we have a void*
which has size 2 bytes. Thus, when the implicit conversion happens from int*
to void*
there will be some loss of information.
So this is the first reason(IMO) that all pointers should have the same size.
- The second reason for having all the pointer to be of the same size is that all pointer hold address. And since for a given machine all addresses have the same size, thus it is very natural(logical) that all pointers should also have the same size.
Using the 2 points above, i think the the second quote, is true.
My first question is what does the C standard say about this.
My second question is that if the C standard does allow pointers to be of different size, then is there a reason for it? I mean allowing pointers to be of different size seems a bit unnatural to me(considering the 2 points i explained above). I am open to being wrong as i am a beginner CS student myself. Please correct me wherever i am wrong. So, i am pretty sure that the standard committee must have already given this(that pointer can have different size) a thought and already have a reason for allowing pointer to have different size. Note that i am asking this(2nd question) only if the standard does allow pointer to have different size. If not, then i am asking on the first question.
CodePudding user response:
Member function pointers can differ:
void* ptr;
size_t (std::string::*mptr)();
std::cout << sizeof(ptr) << '\n';
std::cout << sizeof(mptr) << std::endl;
This printed
8
16
on my system. Background is that member function pointers need to hold additional information e.g. about virtuality etc.
Historically there were systems on which existed 'near' and 'far' pointers which differed in size as well (16 vs. 32 bit) – as far as I am aware of they don't play any role nowadays any more, though.
CodePudding user response:
A few rules:
The sizes of plain-old-data pointers can differ, e.g.
double*
can be (and often is) larger thanint*
. (Think of architectures with off-board floating point units.)void*
must be sufficiently large to hold any pointer type.The size of any non-plain-old-data pointer is the same as any other. In other words
sizeof(myclass*) == sizeof(yourclass*)
.sizeof(const T*)
is the same assizeof(T*)
for anyT
; plain-old-data or otherwiseMember function pointers are not pointers. Pointers to non-member functions, including
static
member functions, are pointers.
CodePudding user response:
Your reasoning in the first case is half-correct. void*
must be able to hold any int*
value. But the reverse is not true. Hence, it's quite possible for void*
to be bigger than int*
.
The statement als gets more complex if you include other pointer types, such as pointers to functions and pointers to methods.
One of the reasons considered by the C Standards committee are DSP chips, where the hardware word size is 16 bits, but char
is implemented as a half-word. This means char*
and void*
need one extra bit compared to short*
and int*
.