Is length of all pointers the same? [duplicate]
Possible Duplicates:
Are there are any platforms where pointers to different types have different sizes? Can the Size of Pointers Vary Depending on what's Pointed To?
Take 32-bit machine for example. It almost is 32-bit long regardless of what type, inclusive of original type or aggregate type. I think the reason is pointer is actually a index in the vitual address space, so it has the same length as machine does.
Howerver, the standard does't require this:
6.2.5.27:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
What is more interesting is that it does't mention the function pointers.
So I want to konw why? Except the truth that in early MS C ,there has so-called far pointer and near pointer, due to the bizarre segment design in early Intel CPU. But nowadays, this is different, the paging and vitual memory will avoid such things.
Another question:
void *
should be casted to any type. So if there is different pointer length. For example, int *
4-byte long and function pointer 8-byte long (Just a example). So how the cast implement? void *
has a fixed bit-mode, will it scale itself to cast into 4-byte int*
and 8-byte function pointer?
The more important issue is "alignment requirements". A char *
could point at any byte, while an int *
may be required to point only at addresses that are a multiple of 4 (if int
is 32 bit)..
Pointers to structures and unions are all constrained to have the same size as additional consequence of C rules. I think it is also the case for pointers to enums.
Pointers to basic types may be of different size.
This has its origin in word adressed machines. Most pointers would simply be the adress of the word, but a char pointer would need to indicates which char in the word is pointed to. (On more modern word adressed architecture such as some DSP, one simply use the word as a char, but in the old days it would have be considered as wastefull and not interoperable with the normal convention on these machines.)
Obviously, void* has to have the same structure as a char*. Assigning a void* to a smaller pointer would simply drop the "char in word" indication. Assigning a smaller pointer to a void* would simply use "first character of the word".
See also: Are there any platforms where pointers to different types have different sizes?
You cannot portably cast between function pointers and void*
pointers, so that part of your question is based on an incorrect assumption.
See this FAQ entry for details.
I also object to the questions' reasoning about "nowadays"; changing C to make it assume that all machines behave as current mainstream hardware is not the proper way to evolve a language. The restrictions on pointer types and pointer conversions exists to capture that there has been, probably are, and very well might again be actual machines where these restrictions make it (too) hard to implement the language.
Pointers to basic types, are different size, but pointers to structures are same size.
精彩评论