It doesn't say they have to be the same size.
If char* and int* have different sizes, then void* cannot be the same size as both of them, yet it is implicitly convertible to both types, back and forth. So no, void* does not have to be the same size.
It doesn't say they have to be the same size.
If char* and int* have different sizes, then void* cannot be the same size as both of them, yet it is implicitly convertible to both types, back and forth. So no, void* does not have to be the same size.
But if void* were to be larger than a char* you would lose data by casting it to char*
And if int* were to be larger than a void* you would lose data by casting to void*
void* and char* must have the same representation. But other pointers can be different sizes. Note that the quote from the standard that you posted said that the results shall compare equal only for the object pointer->void pointer->object pointer. The first bit, void pointer->object pointer, does not guarantee the preservation of value.
See also 6.2.4p26:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 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.
> Does that actually apply to modern computers though?
You mean all the ones you've heard of so far?
The C-FAQ isn't a complete catalogue of all the weird machines out there.
As for real examples, it mentions the x86 - most people have heard of that right?
And given the baffling continuing popularity of TurboC, the 16 bit model can definitely come up with different sized pointers.
Remember, if you intend a career programming C or C++, you're going to come across potentially dozens of different compilers and processors.
Basing everything off "it works for me today" will almost certainly lead to trouble at some point.
> But if void* were to be larger than a char* you would lose data by casting it to char*
C only defines the round trip
T* -> void* -> T*
If you try something like char* to int* (with or without void*), then all sorts of problems can happen.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I guess I misinterpreted the meaning of it then.
Although on a side note I can't see how having different sized pointers would work. A pointer has to be the same size as the cpu registers to be able to address the entire memory space.
Yeah I suppose you are right. I guess I am to used to win32/x86 coding and I'm failing to see the bigger picture. And I also have a bad habbit of being quite stuborn at times
So wait, if all pointer sizes are not the same, then why is it not required to cast malloc()?
Because a pointer to void is convertible to a pointer to any object type.Originally Posted by Epy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks
but if malloc were to allocate memory at an address that is to big for the pointer in question to hold then you'd get very weird results