Recently, I came across the following statement:
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 states that:
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 of the above statements is correct. The second quoted statement looks like it's from the C++ notes of Computer Science, Florida State University.
Here's why, in my opinion all pointers should have the same size:
1) Say we have:
int i = 0;
void* ptr = &i;
Now, suppose the C++ standard 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 side has an int*
which has size 4 bytes while on the left hand side 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.
2) All pointers hold addresses. Since for a given machine all addresses have the same size, it is very natural (logical) that all pointers should also have the same size.
Therefore, I think that the second quote is true.
My first question is what does the C++ standard say about this?
My second question is, 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). So, I am pretty sure that the standard committee must have already given this (that pointers can have different sizes) thought and already have a reason for allowing pointers to have different sizes. Note that I am asking this (2nd question) only if the standard does allow pointers to have different size.