casts vs unions

karron at mcirps2.med.nyu.EDU karron at mcirps2.med.nyu.EDU
Thu Oct 18 08:43:01 AEST 1990


A union is a way to refer to the same location in memory in different ways.  A
union of a char and an int will force alignment of the char and int, and
create un-referenceed dead space.  ( you can create unaligned shorts and ints
with pointers that will cause buss errors that you can not simulate with dbx,
but that is another issue ).

As I understand it, a union is not a run time 'thing', and a union of an
int and, say, a float does not mean that if you assign an int value to the
int, you will not get the correct converted float (normalized value and
exponent). You will see the bit pattern of the int interperted with an
IEEE float template. Correct ?

A cast, on the other hand, take its 'argument', and re-interperts
it as the cast object. It will 'return' the bit pattern of, say, an
int, converted to, san,  a normalized float, with the same value. Is
this correct ? Is there an active conversion of type VALUES ? This is
a run time 'thing'.

My reason for raising this issue is in reading binary numbers first as bytes,
then interperting the values as chars, shorts, longs, or floats as directed by
a file header. I want to minimize run time manipulation of the values
with the use of unions if this is indeed how these things work.

dan.

+-----------------------------------------------------------------------------+
| karron at nyu.edu                          Dan Karron                          |
| . . . . . . . . . . . . . .             New York University Medical Center  |
| 560 First Avenue           \ \    Pager <1> (212) 397 9330                  |
| New York, New York 10016    \**\        <2> 10896   <3> <your-number-here>  |
| (212) 340 5210               \**\__________________________________________ |
| Please Note :Soon to move to dan at karron.med.nyu.edu 128.122.135.3  (Mid Oct)|
+-----------------------------------------------------------------------------+



More information about the Comp.sys.sgi mailing list