int vs void *

C

ChokSheak Lau

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 *)?

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.

any expert responses would be appreciated. thanks.

chok
 
E

Emmanuel Delahaye

ChokSheak Lau said:
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 *)?

x86 in memory model large. int are 16-bit and pointers are 32-bit (seg:eek:fs)

Don't write nonportable code as long as an alternative exists.

However, if you have to, document it properly and please don't disseminate
it, but keep the code under control (function, macro...) so that it can be
adapted in a blink.
 
I

Igmar Palsenberg

ChokSheak said:
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 *)?

On almost all machines where void * isn't 32 bits. If you ever port to
embedded applications, it won't work in most cases either.
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.

It's not portable at all. Use a pointer when you pointer, an not an int.



Igmar
 
D

Dan Pop

In said:
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 *)?

Yup, most 64-bit Unix systems use the I32LP64 model, which is incompatible
with your assumption. It didn't work in the days of MS-DOS, either, for
certain memory models: large, huge, compact.
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.

If you need to convert a pointer to an integer type, use unsigned long
instead of int. Or uintptr_t on C99 compilers. No universal guarantee
that it will work, but your chances are MUCH better than with type int.

Dan
 
D

Darrell Grainger

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.
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

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 *)?
Most 64 bits machines.
 
M

Malcolm

ChokSheak Lau said:
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 *)?

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.

any expert responses would be appreciated. thanks.

chok
 
P

Peter Ammon

ChokSheak said:
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 *)?

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.

any expert responses would be appreciated. thanks.

chok

If you insist on doing this, long int can hold void* more often than an
int can, especially on 64 bit machines.

-Peter
 
D

Dan Pop

In said:
If you insist on doing this, long int can hold void* more often than an
int can, especially on 64 bit machines.

And unsigned long can do the job even better. There is little point in
converting pointer values to signed integers.

The exceptions I'm aware of are AS/400 and Windows on AMD64 hardware.
NT on Alpha "solved" the problem by treating the Alpha as a 32-bit chip.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top