FACTOID # 21: 15% of Army recruits from South Dakota are Native American, which is roughly the same percentage for female Army recruits in the state.
 
 Home   Encyclopedia   Statistics   States A-Z   Flags   Maps   FAQ   About 
   
 
WHAT'S NEW
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Type punning

In computer science, type punning is a common term for any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language. Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ... In computer science, a type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. ... A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ...


In C and C++, constructs such as type conversion, union, and reinterpret_cast 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 implementation-defined behaviour, but the effect in practice is usually to permit type punning. (See the floating-point example below.) Wikibooks has a book on the topic of C Programming The C programming language (often, just C) is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose, programming language with high-level and low-level capabilities. ... In computer science, type conversion or typecasting refers to changing an entity of one data type into another. ... In computer science, a union is a data structure that stores one of several types of data at a single location. ... In computer science, type punning is a common term for any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language. ...


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 an IP address is declared as follows: The Berkeley sockets application programming interface (API) comprises a library for developing applications in the C programming language that perform inter-process communication, most commonly across a computer network. ... This article or section does not cite any references or sources. ...

 int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);  

The bind function is usually called as follows:

 struct sockaddr_in sa = {0}; int sockfd = ...; sa.sin_family = AF_INET; sa.sin_port = htons(port); bind(sockfd, (struct sockaddr *)&sa, sizeof sa);  

The Berkeley sockets library fundamentally relies on the fact that in C, a pointer to struct sockaddr_in is freely convertible to a pointer to struct sockaddr; and, in addition, that the two structure types share the same memory layout. Therefore, a reference to the structure field my_addr->sin_family (where my_addr is of type struct sockaddr*) will actually refer to the field sa.sin_family (where sa is of type struct sockaddr_in). In other words, the sockets library uses type punning to implement a rudimentary form of inheritance. Wikibooks has a book on the topic of C Programming The C programming language (often, just C) is a general-purpose, procedural, imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ... This article or section does not cite any references or sources. ...


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: This article or section is in need of attention from an expert on the subject. ...

 bool is_negative(float x) { return (x < 0.0)? 1: 0; }  

However, supposing that floating-point comparisons are expensive, and also supposing that float is represented according to the IEEE floating-point standard, and integers are 32 bits wide, we could engage in type punning to extract the sign bit of the floating-point number using only integer operations: The IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) is the most widely-used standard for floating-point computation, and is followed by many CPU and FPU implementations. ... In computer science the sign bit is the bit in a computer numbering format which indicates the sign of the number. ...

 bool is_negative(float x) { unsigned int *ui = (unsigned int *)&x; return (*ui >> 31); }  

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. Programming style refers to a set of rules or guidelines used when writing the source code for a computer program. ... Realtime redirects here. ... Compiler optimization is the process of tuning the output of a compiler to minimize some attribute (or maximize the efficiency) of an executable program. ... An illustration of Java source code with prologue comments indicated in red and inline comments in green. ... In telecommunication, the term maintainability has the following meanings: A characteristic of design and installation, expressed as the probability that an item will be retained in or restored to a specified condition within a given period of time, when the maintenance is performed in accordance with prescribed procedures and resources. ...


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 of float, but also relying on the compiler not to optimize away the write to my_union.d. In computer science, a union is a data structure that stores one of several types of data at a single location. ...

 bool is_negative(float x) { union { unsigned int ui; float d; } my_union; my_union.d = x; return (my_union.ui >> 31); }  


For another example of type punning, see Stride of an array. The stride of an array, in computer programming, refers to the number of locations in memory between successive array elements. ...


External links

  • Section of the GCC manual on -fstrict-aliasing, which defeats some union-based type punning (but not all)
  • Defect Report 257 to the C99 standard, incidentally defining "type punning" in terms of union, and discussing the issues surrounding the implementation-defined behavior of the last example above

 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m