[Eug-lug] LP64

Neil Parker nparker at LLX.COM
Sun Jan 9 00:14:13 PST 2005


Jacob Meuser wrote,
>so I got an amd64 mobo (ASUS KV8 SE Deluxe) and 3000+ CPU.
>
>now, I've got a programming question ;)
>
>If there's code that does
>
>	void *data;
>	...
>	data = (void*)open(...);
>
>would it be "fixed" by adding a intptr_t cast?
>
>	void *data;
>	...
>	data = (void*)(intptr_t)open(...);
>
>it shuts gcc up anyway.
>
>where is intptr_t defined on GNU/Linux?  it's in <inttypes.h> on
>OpenBSD.

OK, that's officially weird.

The correct way to fix that code is probably like this:

     int data;
     ...
     data = open(...);

Remember, open() returns an int (specifically, the file descriptor of the
file that you just opened).  I can think of no sensible reason to cast it to
a pointer--it will never point to anything useful, and deferencing it will
probably earn you an immediate trip to the land of segmentation violation.

And on an LP64 compiler, casting to a pointer is just plain wrong--ints and
pointers aren't the same size anymore, and you don't want to shut gcc up,
because it's trying to tell you something important.

               - Neil Parker


More information about the EUGLUG mailing list