473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

int vs void *

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
Nov 14 '05 #1
8 1813
In 'comp.lang.c', "ChokSheak Lau" <ch*******@yahoo.com> wrote:
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:ofs)

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.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2
ChokSheak Lau wrote:
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
Nov 14 '05 #3
In <cc**********@news-int.gatech.edu> "ChokSheak Lau" <ch*******@yahoo.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
On Fri, 9 Jul 2004, ChokSheak Lau wrote:
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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #5
On Fri, 09 Jul 2004 07:17:57 -0400, ChokSheak Lau wrote:
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.

Nov 14 '05 #6

"ChokSheak Lau" <ch*******@yahoo.com> wrote in message
news:cc**********@news-int.gatech.edu...
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

Nov 14 '05 #7
ChokSheak Lau wrote:
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

--
Pull out a splinter to reply.
Nov 14 '05 #8
In <eB*******************@newssvr27.news.prodigy.co m> Peter Ammon <ge******@splintermac.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
18
by: Giannis Papadopoulos | last post by:
According to the standard (ISO C99 draft WG14/N1124), void is the incomplete type that cannot be completed and comprises the empty set of values. Since it declares the absense of a value can it be...
4
by: brianhray | last post by:
Hello: I am writing a C interface and was curious how/why a void* can be used as a reference parameter. //////////////////////////////////////////////////////////// // WORKS: // Client1.h...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.