* Making programs 64 bit clean
@ 1998-06-22 15:36 Tim Jenkins
1998-06-22 18:09 ` Paul Tomblin
1998-06-22 19:27 ` Christopher Curtis
0 siblings, 2 replies; 10+ messages in thread
From: Tim Jenkins @ 1998-06-22 15:36 UTC (permalink / raw)
To: axp-list
I have several programs which cast pointers to ints, which I am hoping to
clean up. It appears that if I redefine the integer variables as longs
and then cast the pointers to longs, I am converting a 64 bit pointer to a
64 bit long, rather than a 64 bit pointer to a 32 bit int.
e.g.
Unacceptable:
int foo;
int *bar;
foo = (int) bar;
Acceptable?:
long foo;
int *bar;
foo = (long) bar;
Is this an acceptable solution to making programs 64 bit clean and is it
portable across 32 bit and 64 bit platforms?
thanks,
tim
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 15:36 Making programs 64 bit clean Tim Jenkins
@ 1998-06-22 18:09 ` Paul Tomblin
1998-06-22 19:27 ` Christopher Curtis
1 sibling, 0 replies; 10+ messages in thread
From: Paul Tomblin @ 1998-06-22 18:09 UTC (permalink / raw)
To: axp-list
Quoting Tim Jenkins (tim@rnaworld.princeton.edu):
>
> I have several programs which cast pointers to ints, which I am hoping to
> clean up. It appears that if I redefine the integer variables as longs
> and then cast the pointers to longs, I am converting a 64 bit pointer to a
> 64 bit long, rather than a 64 bit pointer to a 32 bit int.
> e.g.
According to (my memory of) the ANSI standard, converting from a pointer to an
int is not safe. Period. If you want to be safe (i.e portable), use a void*
as your data type to store pointers, not ints or longs. One way to do this
would be
typedef union int_or_ptr
{
long intval;
void* ptrval;
} int_or_ptr_t;
int_or_ptr_t var;
var.ptrval = some_pointer;
or
var.intval = 12;
It would then be up to you to make sure you only used var.ptrval when you'd
stored a pointer then, and use var.intval when you'd stored one of the
integral data types.
--
Paul Tomblin, ptomblin@xcski.com.
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners" - Ernst Jan Plugge
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: Making programs 64 bit clean
1998-06-22 15:36 Making programs 64 bit clean Tim Jenkins
1998-06-22 18:09 ` Paul Tomblin
@ 1998-06-22 19:27 ` Christopher Curtis
1 sibling, 0 replies; 10+ messages in thread
From: Christopher Curtis @ 1998-06-22 19:27 UTC (permalink / raw)
To: axp-list
On Mon, 22 Jun 1998, Tim Jenkins wrote:
> long foo;
> int *bar;
>
> foo = (long) bar;
>
>
> Is this an acceptable solution to making programs 64 bit clean and is it
> portable across 32 bit and 64 bit platforms?
Basically, no. If you need a pointer, use a pointer. If you need an
int/long use an int/long. The only really portable way to do this is to
union them and use it as such. The only time it's really "safe" to use
casts that change types (eg, excluding signed/unsigned) is when you have a
variable argument list or void pointers. The bummer is that function
pointers can't be converted into void pointers portably. :(
regards,
--
Christopher Curtis - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology - System Administrator, Programmer
Melbourne, Florida USA - http://www.lp.org/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
@ 1998-06-22 17:35 Greg Lindahl
1998-06-22 19:32 ` Michal Jaegermann
1998-06-22 19:53 ` Richard Hadsell
0 siblings, 2 replies; 10+ messages in thread
From: Greg Lindahl @ 1998-06-22 17:35 UTC (permalink / raw)
To: axp-list
> Unacceptable:
>
> int foo;
> int *bar;
>
> foo = (int) bar;
>
> Acceptable?:
>
> long foo;
> int *bar;
>
> foo = (long) bar;
>
>
> Is this an acceptable solution to making programs 64 bit clean and is it
> portable across 32 bit and 64 bit platforms?
This is the easiest way to fix broken code. However, it isn't
necessarily portable to other 64 bit machines -- I think everyone
else (?) defines long == 32 bits and long long == 64 bits.
Your best bet for a clean, easy solution is to typedef an int64 type
and use that, with an appropriate definition for each machine. If you
are clever you can put that into a configure script...
It's much better in the long run to fix the programs to not cast
pointers to ints of any width, but of course that is much more
expensive than this quick hack.
-- g
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 17:35 Greg Lindahl
@ 1998-06-22 19:32 ` Michal Jaegermann
1998-06-22 20:47 ` Christopher Curtis
1998-06-22 19:53 ` Richard Hadsell
1 sibling, 1 reply; 10+ messages in thread
From: Michal Jaegermann @ 1998-06-22 19:32 UTC (permalink / raw)
To: axp-list
Greg Lindahl writes:
>
> This is the easiest way to fix broken code. However, it isn't
> necessarily portable to other 64 bit machines -- I think everyone
> else (?) defines long == 32 bits and long long == 64 bits.
This "everyone else" does not include Digital Unix, for example,
nor Sparc Linux. Maybe there are some examples in this category
but I do not know them (NT on Alpha is 32 bit - in case you wonder).
> Your best bet for a clean, easy solution is to typedef an int64 type
> and use that, with an appropriate definition for each machine.
If you have rely on some type widths then the above is much better.
C Standard says only that short is not wider than int and int is not
wider than long. Nothing more! It does not even guarantees that there
exists an integer type with the same 'sizeof' as a pointer.
For overlaying objects of different types C provides a union construct
which is, traditionally, grossly underutilized.
> It's much better in the long run to fix the programs to not cast
> pointers to ints of any width, but of course that is much more
> expensive than this quick hack.
Agreed. Unfortunately X, for example, uses pointers all over the place
as "universal containters" and there is no good way to change that.
I strongly suspect that recent problems with servers which work only
in 8 colours have something to do with that.
Michal
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 19:32 ` Michal Jaegermann
@ 1998-06-22 20:47 ` Christopher Curtis
1998-06-22 21:49 ` Michal Jaegermann
1998-06-22 21:50 ` Paul Tomblin
0 siblings, 2 replies; 10+ messages in thread
From: Christopher Curtis @ 1998-06-22 20:47 UTC (permalink / raw)
To: axp-list
On Mon, 22 Jun 1998, Michal Jaegermann wrote:
> Agreed. Unfortunately X, for example, uses pointers all over the place
> as "universal containters" and there is no good way to change that.
... yet another reason to replace X with a more modern system.
:(
--
Christopher Curtis - http://www.ee.fit.edu/users/ccurtis
Florida Institute of Technology - System Administrator, Programmer
Melbourne, Florida USA - http://www.lp.org/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 20:47 ` Christopher Curtis
@ 1998-06-22 21:49 ` Michal Jaegermann
1998-06-22 21:50 ` Paul Tomblin
1 sibling, 0 replies; 10+ messages in thread
From: Michal Jaegermann @ 1998-06-22 21:49 UTC (permalink / raw)
To: axp-list
Christopher Curtis writes:
>
> > Unfortunately X, for example, uses pointers all over the place
> > as "universal containters" and there is no good way to change that.
>
> ... yet another reason to replace X with a more modern system.
>
Any examples, preferably with at least a working prototype, of
this "more modern system"?
--mj
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 20:47 ` Christopher Curtis
1998-06-22 21:49 ` Michal Jaegermann
@ 1998-06-22 21:50 ` Paul Tomblin
1998-06-22 22:24 ` Jeff Sturm
1 sibling, 1 reply; 10+ messages in thread
From: Paul Tomblin @ 1998-06-22 21:50 UTC (permalink / raw)
To: axp-list
Quoting Christopher Curtis (ccurtis@ee.fit.edu):
> On Mon, 22 Jun 1998, Michal Jaegermann wrote:
> > Agreed. Unfortunately X, for example, uses pointers all over the place
> > as "universal containters" and there is no good way to change that.
>
> ... yet another reason to replace X with a more modern system.
If there were such a thing...
Note that in order to produce such a thing it would have to be:
- network transparent
- portable as all hell
- upwards compatible with X servers/clients.
- truely object oriented instead of the half-assed "objects in C" like Xt and
Motif currently do.
- Good support for accelerated hardware without cutting out "normal" hardware.
I think you could achieve something like this with a new API that still spoke
X protocol underneath, providing it wasn't any slower than raw X. Implement
it in C++ and Java to start, then turn it over to the community to do "my
favourite language" bindings.
--
Paul Tomblin, ptomblin@xcski.com.
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners" - Ernst Jan Plugge
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 21:50 ` Paul Tomblin
@ 1998-06-22 22:24 ` Jeff Sturm
0 siblings, 0 replies; 10+ messages in thread
From: Jeff Sturm @ 1998-06-22 22:24 UTC (permalink / raw)
To: axp-list
On Mon, 22 Jun 1998, Paul Tomblin wrote:
> Quoting Christopher Curtis (ccurtis@ee.fit.edu):
> > On Mon, 22 Jun 1998, Michal Jaegermann wrote:
> > > Agreed. Unfortunately X, for example, uses pointers all over the place
> > > as "universal containters" and there is no good way to change that.
> >
> > ... yet another reason to replace X with a more modern system.
>
> If there were such a thing...
>
> Note that in order to produce such a thing it would have to be:
> - network transparent
> - portable as all hell
> - upwards compatible with X servers/clients.
> - truely object oriented instead of the half-assed "objects in C" like Xt and
> Motif currently do.
> - Good support for accelerated hardware without cutting out "normal" hardware.
> I think you could achieve something like this with a new API that still spoke
> X protocol underneath, providing it wasn't any slower than raw X. Implement
> it in C++ and Java to start, then turn it over to the community to do "my
> favourite language" bindings.
It just might happen, now that XFree86 and TOG have split... now they are
"free" to continue to develop X11R6.3 into anything they want.
X certainly could use an overhaul. But starting from scratch would a
very, very bad idea. There's a lot of good code in there already.
(I have a side project to reimplement Xlib in pure java. I was suprised
at how many simple optimizations can be made... Xlib is a very ugly library.
The low-level X protocol is basically sound, it's just missing a few
essential features of modern window systems, like matrix transformations
for one...)
Jeff
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Making programs 64 bit clean
1998-06-22 17:35 Greg Lindahl
1998-06-22 19:32 ` Michal Jaegermann
@ 1998-06-22 19:53 ` Richard Hadsell
1 sibling, 0 replies; 10+ messages in thread
From: Richard Hadsell @ 1998-06-22 19:53 UTC (permalink / raw)
To: axp-list
Greg Lindahl wrote:
>
> > Is this an acceptable solution to making programs 64 bit clean and is it
> > portable across 32 bit and 64 bit platforms?
>
> This is the easiest way to fix broken code. However, it isn't
> necessarily portable to other 64 bit machines -- I think everyone
> else (?) defines long == 32 bits and long long == 64 bits.
>
> Your best bet for a clean, easy solution is to typedef an int64 type
> and use that, with an appropriate definition for each machine. If you
> are clever you can put that into a configure script...
If you have to do it, long has been the traditionally safe way to
convert between integers and pointers. I can't find where I read that,
but the ARM said in sec. 5.4, "A pointer converted to an integer of
sufficient size (if any such exists on the implementation) and back to
the same pointer type will have its original value; ..." I haven't
heard of any platforms for which long is smaller than a pointer; long
long is not yet supported on some compilers, so I wouldn't rely at all
on its portability. So I use long, when necessary. Of course, I do it
through a typedef (iptr) that I define in a header that has other
machine-dependent typedefs. I wouldn't waste space and performance by
forcing 32-bit pointers through an int64.
--
Dick Hadsell 914-381-8400 Fax: 914-381-9790
Reply-to: hadsell@blueskystudios.com
Blue Sky | VIFX http://www.blueskystudios.com
1 South Road, Harrison, NY 10528
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~1998-06-22 22:25 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-06-22 15:36 Making programs 64 bit clean Tim Jenkins
1998-06-22 18:09 ` Paul Tomblin
1998-06-22 19:27 ` Christopher Curtis
-- strict thread matches above, loose matches on Subject: below --
1998-06-22 17:35 Greg Lindahl
1998-06-22 19:32 ` Michal Jaegermann
1998-06-22 20:47 ` Christopher Curtis
1998-06-22 21:49 ` Michal Jaegermann
1998-06-22 21:50 ` Paul Tomblin
1998-06-22 22:24 ` Jeff Sturm
1998-06-22 19:53 ` Richard Hadsell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox