<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
type	B-Language
punning	I-Language
is	O
any	O
programming	O
technique	O
that	O
subverts	O
or	O
circumvents	O
the	O
type	O
system	O
of	O
a	O
programming	O
language	O
in	O
order	O
to	O
achieve	O
an	O
effect	O
that	O
would	O
be	O
difficult	O
or	O
impossible	O
to	O
achieve	O
within	O
the	O
bounds	O
of	O
the	O
formal	O
language	O
.	O
</s>
<s>
In	O
C	B-Language
and	O
C++	B-Language
,	O
constructs	O
such	O
as	O
pointer	O
type	O
conversion	O
and	O
union	O
—	O
C++	B-Language
adds	O
reference	O
type	O
conversion	O
and	O
reinterpret_cast	O
to	O
this	O
list	O
—	O
are	O
provided	O
in	O
order	O
to	O
permit	O
many	O
kinds	O
of	O
type	B-Language
punning	I-Language
,	O
although	O
some	O
kinds	O
are	O
not	O
actually	O
supported	O
by	O
the	O
standard	O
language	O
.	O
</s>
<s>
In	O
the	O
Pascal	B-Application
programming	I-Application
language	I-Application
,	O
the	O
use	O
of	O
records	O
with	O
variants	B-Language
may	O
be	O
used	O
to	O
treat	O
a	O
particular	O
data	O
type	O
in	O
more	O
than	O
one	O
manner	O
,	O
or	O
in	O
a	O
manner	O
not	O
normally	O
permitted	O
.	O
</s>
<s>
One	O
classic	O
example	O
of	O
type	B-Language
punning	I-Language
is	O
found	O
in	O
the	O
Berkeley	B-Protocol
sockets	I-Protocol
interface	O
.	O
</s>
<s>
The	O
function	O
to	O
bind	O
an	O
opened	O
but	O
uninitialized	O
socket	O
to	O
an	O
IP	B-Protocol
address	I-Protocol
is	O
declared	O
as	O
follows	O
:	O
</s>
<s>
The	O
Berkeley	B-Protocol
sockets	I-Protocol
library	O
fundamentally	O
relies	O
on	O
the	O
fact	O
that	O
in	O
C	B-Language
,	O
a	O
pointer	O
to	O
struct	B-Application
sockaddr_in	O
is	O
freely	O
convertible	O
to	O
a	O
pointer	O
to	O
struct	B-Application
sockaddr	O
;	O
and	O
,	O
in	O
addition	O
,	O
that	O
the	O
two	O
structure	O
types	O
share	O
the	O
same	O
memory	O
layout	O
.	O
</s>
<s>
Therefore	O
,	O
a	O
reference	O
to	O
the	O
structure	O
field	O
my_addr->sin_family	O
(	O
where	O
my_addr	O
is	O
of	O
type	O
struct	B-Application
sockaddr*	O
)	O
will	O
actually	O
refer	O
to	O
the	O
field	O
sa.sin_family	O
(	O
where	O
sa	O
is	O
of	O
type	O
struct	B-Application
sockaddr_in	O
)	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
the	O
sockets	O
library	O
uses	O
type	B-Language
punning	I-Language
to	O
implement	O
a	O
rudimentary	O
form	O
of	O
polymorphism	B-Application
or	O
inheritance	B-Language
.	O
</s>
<s>
Not	O
all	O
examples	O
of	O
type	B-Language
punning	I-Language
involve	O
structures	O
,	O
as	O
the	O
previous	O
example	O
did	O
.	O
</s>
<s>
Suppose	O
we	O
want	O
to	O
determine	O
whether	O
a	O
floating-point	B-Algorithm
number	I-Algorithm
is	O
negative	O
.	O
</s>
<s>
However	O
,	O
supposing	O
that	O
floating-point	B-Algorithm
comparisons	O
are	O
expensive	O
,	O
and	O
also	O
supposing	O
that	O
float	B-Algorithm
is	O
represented	O
according	O
to	O
the	O
IEEE	O
floating-point	B-Algorithm
standard	O
,	O
and	O
integers	O
are	O
32	O
bits	O
wide	O
,	O
we	O
could	O
engage	O
in	O
type	B-Language
punning	I-Language
to	O
extract	O
the	O
sign	B-Algorithm
bit	I-Algorithm
of	O
the	O
floating-point	B-Algorithm
number	I-Algorithm
using	O
only	O
integer	O
operations	O
:	O
</s>
<s>
Note	O
that	O
the	O
behaviour	O
will	O
not	O
be	O
exactly	O
the	O
same	O
:	O
in	O
the	O
special	O
case	O
of	O
x	O
being	O
negative	B-Algorithm
zero	I-Algorithm
,	O
the	O
first	O
implementation	O
yields	O
false	O
while	O
the	O
second	O
yields	O
true	O
.	O
</s>
<s>
Also	O
,	O
the	O
first	O
implementation	O
will	O
return	O
false	O
for	O
any	O
NaN	O
value	O
,	O
but	O
the	O
latter	O
might	O
return	O
true	O
for	O
NaN	O
values	O
with	O
the	O
sign	B-Algorithm
bit	I-Algorithm
set	O
.	O
</s>
<s>
This	O
kind	O
of	O
type	B-Language
punning	I-Language
is	O
more	O
dangerous	O
than	O
most	O
.	O
</s>
<s>
Whereas	O
the	O
former	O
example	O
relied	O
only	O
on	O
guarantees	O
made	O
by	O
the	O
C	B-Language
programming	I-Language
language	I-Language
about	O
structure	O
layout	O
and	O
pointer	O
convertibility	O
,	O
the	O
latter	O
example	O
relies	O
on	O
assumptions	O
about	O
a	O
particular	O
system	O
's	O
hardware	O
.	O
</s>
<s>
Some	O
situations	O
,	O
such	O
as	O
time-critical	B-General_Concept
code	O
that	O
the	O
compiler	O
otherwise	O
fails	O
to	O
optimize	B-Application
,	O
may	O
require	O
dangerous	O
code	O
.	O
</s>
<s>
Practical	O
examples	O
of	O
floating-point	B-Algorithm
punning	O
include	O
fast	O
inverse	O
square	O
root	O
popularized	O
by	O
Quake	B-Application
III	I-Application
,	O
fast	O
FP	O
comparison	O
as	O
integers	O
,	O
and	O
finding	O
neighboring	O
values	O
by	O
incrementing	O
as	O
an	O
integer	O
(	O
implementing	O
)	O
.	O
</s>
<s>
In	O
addition	O
to	O
the	O
assumption	O
about	O
bit-representation	O
of	O
floating-point	B-Algorithm
numbers	I-Algorithm
,	O
the	O
above	O
floating-point	B-Algorithm
type-punning	O
example	O
also	O
violates	O
the	O
C	B-Language
language	I-Language
's	O
constraints	O
on	O
how	O
objects	O
are	O
accessed	O
:	O
the	O
declared	O
type	O
of	O
x	O
is	O
float	B-Algorithm
but	O
it	O
is	O
read	O
through	O
an	O
expression	O
of	O
type	O
unsigned	O
int	O
.	O
</s>
<s>
On	O
many	O
common	O
platforms	O
,	O
this	O
use	O
of	O
pointer	O
punning	O
can	O
create	O
problems	O
if	O
different	O
pointers	O
are	O
aligned	B-Application
in	I-Application
machine-specific	I-Application
ways	I-Application
.	O
</s>
<s>
Furthermore	O
,	O
pointers	O
of	O
different	O
sizes	O
can	O
alias	B-Application
accesses	I-Application
to	I-Application
the	I-Application
same	I-Application
memory	I-Application
,	O
causing	O
problems	O
that	O
are	O
unchecked	O
by	O
the	O
compiler	O
.	O
</s>
<s>
Even	O
when	O
data	O
size	O
and	O
pointer	O
representation	O
match	O
,	O
however	O
,	O
compilers	O
can	O
rely	O
on	O
the	O
non-aliasing	O
constraints	O
to	O
perform	O
optimizations	O
that	O
would	O
be	O
unsafe	O
in	O
the	O
presence	O
of	O
disallowed	O
aliasing	B-Application
.	O
</s>
<s>
According	O
to	O
the	O
C	B-Language
standard	O
,	O
this	O
code	O
should	O
not	O
(	O
or	O
rather	O
,	O
does	O
not	O
have	O
to	O
)	O
compile	O
,	O
however	O
,	O
if	O
it	O
does	O
,	O
then	O
piAsRawData	O
typically	O
contains	O
the	O
raw	O
bits	O
of	O
pi	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
but	O
not	O
in	O
C++	B-Language
,	O
it	O
is	O
sometimes	O
possible	O
to	O
perform	O
type	B-Language
punning	I-Language
via	O
a	O
union	O
.	O
</s>
<s>
(	O
The	O
following	O
example	O
assumes	O
IEEE-754	O
bit-representation	O
for	O
type	O
float	B-Algorithm
.	O
)	O
</s>
<s>
Accessing	O
my_union.ui	O
after	O
most	O
recently	O
writing	O
to	O
the	O
other	O
member	O
,	O
my_union.d	O
,	O
is	O
an	O
allowed	O
form	O
of	O
type-punning	O
in	O
C	B-Language
,	O
provided	O
that	O
the	O
member	O
read	O
is	O
not	O
larger	O
than	O
the	O
one	O
whose	O
value	O
was	O
set	O
(	O
otherwise	O
the	O
read	O
has	O
unspecified	B-Language
behavior	I-Language
)	O
.	O
</s>
<s>
The	O
same	O
is	O
syntactically	O
valid	O
but	O
has	O
undefined	B-Language
behavior	I-Language
in	O
C++	B-Language
,	O
however	O
,	O
where	O
only	O
the	O
last-written	O
member	O
of	O
a	O
union	O
is	O
considered	O
to	O
have	O
any	O
value	O
at	O
all	O
.	O
</s>
<s>
For	O
another	O
example	O
of	O
type	B-Language
punning	I-Language
,	O
see	O
Stride	B-Data_Structure
of	I-Data_Structure
an	I-Data_Structure
array	I-Data_Structure
.	O
</s>
<s>
In	O
Pascal	B-Application
,	O
copying	O
a	O
real	O
to	O
an	O
integer	O
converts	O
it	O
to	O
the	O
truncated	O
value	O
.	O
</s>
<s>
This	O
method	O
would	O
translate	O
the	O
binary	O
value	O
of	O
the	O
floating-point	B-Algorithm
number	I-Algorithm
into	O
whatever	O
it	O
is	O
as	O
a	O
long	O
integer	O
(	O
32	O
bit	O
)	O
,	O
which	O
will	O
not	O
be	O
the	O
same	O
and	O
may	O
be	O
incompatible	O
with	O
the	O
long	O
integer	O
value	O
on	O
some	O
systems	O
.	O
</s>
<s>
Where	O
"	O
new	O
"	O
is	O
the	O
standard	O
routine	O
in	O
Pascal	B-Application
for	O
allocating	O
memory	O
for	O
a	O
pointer	O
,	O
and	O
"	O
hex	O
"	O
is	O
presumably	O
a	O
routine	O
to	O
print	O
the	O
hexadecimal	O
string	O
describing	O
the	O
value	O
of	O
an	O
integer	O
.	O
</s>
<s>
The	O
reinterpret	B-Language
cast	I-Language
technique	O
from	O
C/C	O
++	O
also	O
works	O
in	O
Pascal	B-Application
.	O
</s>
<s>
reading	O
dwords	O
from	O
a	O
byte	O
stream	O
,	O
and	O
we	O
want	O
to	O
treat	O
them	O
as	O
float	B-Algorithm
.	O
</s>
<s>
Here	O
is	O
a	O
working	O
example	O
,	O
where	O
we	O
reinterpret-cast	O
a	O
dword	O
to	O
a	O
float	B-Algorithm
:	O
</s>
<s>
In	O
C#	B-Application
(	O
and	O
other	O
.NET	O
languages	O
)	O
,	O
type	B-Language
punning	I-Language
is	O
a	O
little	O
harder	O
to	O
achieve	O
because	O
of	O
the	O
type	O
system	O
,	O
but	O
can	O
be	O
done	O
nonetheless	O
,	O
using	O
pointers	O
or	O
struct	B-Application
unions	O
.	O
</s>
<s>
C#	B-Application
only	O
allows	O
pointers	O
to	O
so-called	O
native	O
types	O
,	O
i.e.	O
</s>
<s>
any	O
primitive	O
type	O
(	O
except	O
string	O
)	O
,	O
enum	O
,	O
array	O
or	O
struct	B-Application
that	O
is	O
composed	O
only	O
of	O
other	O
native	O
types	O
.	O
</s>
<s>
Struct	B-Application
unions	O
are	O
allowed	O
without	O
any	O
notion	O
of	O
'	O
unsafe	O
 '	O
code	O
,	O
but	O
they	O
do	O
require	O
the	O
definition	O
of	O
a	O
new	O
type	O
.	O
</s>
<s>
Raw	O
CIL	O
can	O
be	O
used	O
instead	O
of	O
C#	B-Application
,	O
because	O
it	O
does	O
n't	O
have	O
most	O
of	O
the	O
type	O
limitations	O
.	O
</s>
<s>
The	O
cpblk	O
CIL	O
opcode	O
allows	O
for	O
some	O
other	O
tricks	O
,	O
such	O
as	O
converting	O
a	O
struct	B-Application
to	O
a	O
byte	O
array	O
:	O
</s>
