hi, this should be more of a systems compatibility question. Using casts
between type int and void *, on roughly how many different kinds of machines
would it not work? means do you know of any machine in which sizeof(int) !=
sizeof(void *)?
The TMS320C5510 DSP core found in the OMAP processor (used in the Palm
Tungsten PDA) can be configured so that sizeof(int) != sizeof(void *).
Are there any other examples? Definitely. Does it matter? Not to me. If
I'm going to write a piece of code that might live for twenty years (or
even five) then how can I determine that it will never get ported to a
platform that will violate some assumptions I have made.
If I had developed code for the TMS320C542 I could have safely assumed
that sizeof(int) == sizeof(void *). Would I have ever imaged ten years ago
that Texas Instruments was going to create a TMS320C54xx and TMS320C55xx
family of processors to replace the TMS320C54x and where sizeof(int) !=
sizeof(void *)? No.
So it is entirely possible that I coding ten years ago for a project that
used the TMS320C542 and now the company wants to expand the capabilities.
They need more memory so they switch the TMS320C549 with extended memory.
It uses the large memory model where sizeof(int) != sizeof(void *). They
recompile my code assuming it will continue to work. I might not even be
there. Even if I was, would I remember an assumption I made ten years ago?
Probably not.
What if it was being used for a medical device? Obviously, you'd be more
careful knowing it was used for a medical device. What if you were writing
a library with no idea where the library was going to be used?
Bottom line, do it right the first time. Shortcuts are good for short term
but long term they could prove disasterous.
i'm wondering how portable it is to write code that treats int and void * in
the interchangeably. apparently gcc used to do that, but i'm not sure that
had changed that or not.
Unless you can put something in the code such that it will not compile on
a different platform, it is unwise to code assuming platform specifically.
A more real life example, Commodore created an MC68000 box (Amiga).
Microsoft wrote a version of BASIC for it. The processor at the time had a
24-bit address bus. Microsoft assumed this would always be true. Just
three years later the architecture changed to a 32-bit address bus
(MC68020). Microsoft BASIC stopped working on that machine. They never
fixed it. Everyone programming BASIC switched to C, C++ or a different
platform.