- Type punning
__FORCETOC__In
computer science , type punning is a common term for any programming technique that subverts or circumvents thetype system of aprogramming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language.In C and
C++ , constructs such astype conversion ,union
, and
are provided in order to permit many kinds of type punning, although some kinds are not actually supported by the standard language. For example, reading from a different union member than the last one written invokes undefined behavior, but the effect in practice is usually to permit type punning. (See the floating-point example below.)reinterpret_cast Sockets example
One classic example of type punning is found in the
Berkeley sockets interface. The function to bind an opened but uninitialized socket to anIP address is declared as follows:The
bind
function is usually called as follows:The Berkeley sockets library fundamentally relies on the fact that in C, a pointer to
struct sockaddr_in
is freely convertible to a pointer tostruct sockaddr
; and, in addition, that the two structure types share the same memory layout. Therefore, a reference to the structure fieldmy_addr->sin_family
(wheremy_addr
is of typestruct sockaddr*
) will actually refer to the fieldsa.sin_family
(wheresa
is of typestruct sockaddr_in
). In other words, the sockets library uses type punning to implement a rudimentary form of inheritance.Floating-point example
Not all examples of type punning involve structures, as the previous example did. Suppose we want to determine whether a
floating-point number is negative. We could write:However, supposing that floating-point comparisons are expensive, and also supposing that
float
is represented according to theIEEE floating-point standard , and integers are 32 bits wide, we could engage in type punning to extract thesign bit of the floating-point number using only integer operations:Although most
programming style guides frown on any kind of type punning, this kind of type punning is more dangerous than most. Whereas the former relied only on guarantees made by the C programming language about structure layout and pointer convertibility, this example relies on assumptions about a system's particular hardware. Some situations, such as time-critical code that the compiler otherwise fails to optimize, may require dangerous code. In these cases, documenting all such assumptions in comments helps to keep the code maintainable.Even more dangerous than the punning implementation above is the following implementation, which tries to use C's union data type to retrieve the integer representation of
x
. In this case, the code is not just relying on the hardware representation offloat
, but also relying on the compiler not to optimize away the write tomy_union.d
.For another example of type punning, see
Stride of an array .External links
* [http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Optimize-Options.html#index-fstrict_002daliasing-542 Section] of the GCC manual on -fstrict-aliasing, which defeats some type punning
* [http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_257.htm Defect Report 257] to theC99 standard, incidentally defining "type punning" in terms ofunion
, and discussing the issues surrounding the implementation-defined behavior of the last example above
Wikimedia Foundation. 2010.