Type Punning

last modified: November 21, 2014

A facility of some programming languages where the TypeSystem can be intentionally subverted, typically to treat values of one type as if they were another.

For example, the following C struct...

union {
  unsigned int bits;
  float number;
},

...defines bits and number so they share the same memory. Thus, assigning a value to number results in a value being in bits. This can be usefully exploited to determine (for example) whether number is negative or not by using bits to examine the bit pattern of number.


Loading...