HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a union is a
value Value or values may refer to: Ethics and social * Value (ethics) wherein said concept may be construed as treating actions themselves as abstract objects, associating value to them ** Values (Western philosophy) expands the notion of value beyo ...
that may have any of several representations or formats within the same position in
memory Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remember ...
; that consists of a variable that may hold such a
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, ...
. Some
programming languages A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
support special
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s, called union types, to describe such values and variables. In other words, a union type definition will specify which of a number of permitted primitive types may be stored in its instances, e.g., "float or long integer". In contrast with a record (or structure), which could be defined to contain both a float ''and'' an integer; in a union, there is only one value at any given time. A union can be pictured as a chunk of memory that is used to store variables of different data types. Once a new value is assigned to a field, the existing data is overwritten with the new data. The memory area storing the value has no intrinsic type (other than just
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable uni ...
s or
words A word is a basic element of language that carries an objective or practical meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no conse ...
of memory), but the value can be treated as one of several abstract data types, having the type of the value that was last written to the memory area. In
type theory In mathematics, logic, and computer science, a type theory is the formal presentation of a specific type system, and in general type theory is the academic study of type systems. Some type theories serve as alternatives to set theory as a founda ...
, a union has a sum type; this corresponds to
disjoint union In mathematics, a disjoint union (or discriminated union) of a family of sets (A_i : i\in I) is a set A, often denoted by \bigsqcup_ A_i, with an injection of each A_i into A, such that the images of these injections form a partition of A ( ...
in mathematics. Depending on the language and type, a union value may be used in some operations, such as assignment and comparison for equality, without knowing its specific type. Other operations may require that knowledge, either by some external information, or by the use of a tagged union.


Untagged unions

Because of the limitations of their use, untagged unions are generally only provided in untyped languages or in a type-unsafe way (as in C). They have the advantage over simple tagged unions of not requiring space to store a data type tag. The name "union" stems from the type's formal definition. If a type is considered as the
set Set, The Set, SET or SETS may refer to: Science, technology, and mathematics Mathematics *Set (mathematics), a collection of elements *Category of sets, the category whose objects and morphisms are sets and total functions, respectively Electro ...
of all values that that type can take on, a union type is simply the mathematical union of its constituting types, since it can take on any value any of its fields can. Also, because a mathematical union discards duplicates, if more than one field of the union can take on a single common value, it is impossible to tell from the value alone which field was last written. However, one useful programming function of unions is to map smaller data elements to larger ones for easier manipulation. A data structure consisting, for example, of 4 bytes and a 32-bit integer, can form a union with an unsigned 64-bit integer, and thus be more readily accessed for purposes of comparison etc.


Unions in various programming languages


ALGOL 68

ALGOL 68 ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously d ...
has tagged unions, and uses a case clause to distinguish and extract the constituent type at runtime. A union containing another union is treated as the set of all its constituent possibilities, and if the context requires it a union is automatically coerced into the wider union. A union can explicitly contain no value, which can be distinguished at runtime. An example is: mode node = union (real, int, string, void); node n := "abc"; case n in (real r): print(("real:", r)), (int i): print(("int:", i)), (string s): print(("string:", s)), (void): print(("void:", "EMPTY")), out print(("?:", n)) esac The syntax of the C/C++ union type and the notion of casts was derived from ALGOL 68, though in an untagged form.


C/C++

In C and C++, untagged unions are expressed nearly exactly like structures ( structs), except that each data member begins at the same location in memory. The data members, as in structures, need not be primitive values, and in fact may be structures or even other unions. C++ (since
C++11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build ...
) also allows for a data member to be any type that has a full-fledged constructor/destructor and/or copy constructor, or a non-trivial copy assignment operator. For example, it is possible to have the standard C++ string as a member of a union. The primary use of a union is allowing access to a common location by different data types, for example hardware input/output access, bitfield and word sharing, or
type punning In computer science, a type punning is 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 ...
. Unions can also provide low-level polymorphism. However, there is no checking of types, so it is up to the programmer to be sure that the proper fields are accessed in different contexts. The relevant field of a union variable is typically determined by the state of other variables, possibly in an enclosing struct. One common C programming idiom uses unions to perform what C++ calls a reinterpret_cast, by assigning to one field of a union and reading from another, as is done in code which depends on the raw representation of the values. A practical example is the method of computing square roots using the IEEE representation. This is not, however, a safe use of unions in general.


Anonymous union

In C++, C11, and as a non-standard extension in many compilers, unions can also be anonymous. Their data members do not need to be referenced, are instead accessed directly. They have some restrictions as opposed to traditional unions: in C11, they must be a member of another structure or union, and in C++, they can not have
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
or access specifiers. Simply omitting the class-name portion of the syntax does not make a union an anonymous union. For a union to qualify as an anonymous union, the declaration must not declare an object. Example: #include #include int main() Anonymous unions are also useful in C struct definitions to provide a sense of namespacing.


Transparent union

In compilers such as GCC, Clang, and IBM XL C for AIX, a attribute is available for union types. Types contained in the union can be converted transparently to the union type itself in a function call, provided that all types have the same size. It is mainly intended for function with multiple parameter interfaces, a use necessitated by early Unix extensions and later re-standarisation.


COBOL

In
COBOL COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily u ...
, union data items are defined in two ways. The first uses the (66 level) keyword, which effectively maps a second alphanumeric data item on top of the same memory location as a preceding data item. In the example code below, data item is defined as a group containing another group and a numeric data item. is defined as an alphanumeric data item that renames , treating the data bytes continued within it as character data. 01 PERSON-REC. 05 PERSON-NAME. 10 PERSON-NAME-LAST PIC X(12). 10 PERSON-NAME-FIRST PIC X(16). 10 PERSON-NAME-MID PIC X. 05 PERSON-ID PIC 9(9) PACKED-DECIMAL. 01 PERSON-DATA RENAMES PERSON-REC. The second way to define a union type is by using the keyword. In the example code below, data item is defined as a 2-byte binary integer containing a version number. A second data item is defined as a two-character alphanumeric variable. Since the second item is ''redefined'' over the first item, the two items share the same address in memory, and therefore share the same underlying data bytes. The first item interprets the two data bytes as a binary value, while the second item interprets the bytes as character values. 01 VERS-INFO. 05 VERS-NUM PIC S9(4) COMP. 05 VERS-BYTES PIC X(2) REDEFINES VERS-NUM


Pascal

In
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
, there are two ways to create unions. One is the standard way through a variant record. The second is a nonstandard means of declaring a variable as absolute, meaning it is placed at the same memory location as another variable or at an absolute address. While all Pascal compilers support variant records, only some support absolute variables. For the purposes of this example, the following are all integer types: a byte is 8-bits, a word is 16-bits, and an integer is 32-bits. The following example shows the non-standard absolute form: VAR A: Integer; B: Array ..4of Byte absolute A; C: Integer absolute 0; In the first example, each of the elements of the array B maps to one of the specific bytes of the variable A. In the second example, the variable C is assigned to the exact machine address 0. In the following example, a record has variants, some of which share the same location as others: TYPE Shape = (Circle, Square, Triangle); Dimensions = record case Figure: Shape of Circle: (Diameter: real); Square: (Width: real); Triangle: (Side: real; Angle1, Angle2: 0..360) end;


PL/I

In
PL/I PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language developed and published by IBM. It is designed for scientific, engineering, business and system programming. I ...
then original term for a union was ''cell'', which is still accepted as a synonym for union by several compilers. The union declaration is similar to the structure definition, where elements at the same level within the union declaration occupy the same storage. Elements of the union can be any data type, including structures and array. Here vers_num and vers_bytes occupy the same storage locations. 1 vers_info union, 5 vers_num fixed binary, 5 vers_bytes pic '(2)A'; An alternative to a union declaration is the DEFINED attribute, which allows alternative declarations of storage, however the data types of the base and defined variables must match.


Syntax and example


C/C++

In C and C++, the syntax is: union ; A structure can also be a member of a union, as the following example shows: union name1 uvar; This example defines a variable uvar as a union (tagged as name1), which contains two members, a structure (tagged as name2) named svar (which in turn contains three members), and an integer variable named d. Unions may occur within structures and arrays, and vice versa: struct symtab
SYM #REDIRECT Sym{{R from other capitalisation The Size Ya Maghas Index (SYM) or body size index is derived from the Body Shape and height of a female between the ages of 18 and 35 in the Maghas Clan of the Elgon Masai Community. The SYM is general ...
The number ival is referred to as symtab u.ival and the first character of string sval by either of *symtab u.sval or symtab u.sval /code>.


PHP

Union types were introduced in PHP 8.0. The values are implicitly "tagged" with a type by the language, and may be retrieved by "gettype()". class Example


Python

Support for typing was introduced in Python 3.5. The new syntax for union types were introduced in Python 3.10. class Example: foo = 0 def square_and_add(self, bar: int , float) -> int , float: return bar ** 2 + self.foo


TypeScript

Union types are supported in TypeScript. The values are implicitly "tagged" with a type by the language, and may be retrieved by "typeof()". function successor(n: number , bigint): number , bigint


See also

* Tagged union * UNION operator * Variant type


References

*


External links


boost::variant
a type-safe alternative to C++ unions
MSDN: Classes,Structures & Unions
for examples and syntax
differences
differences between union & structure
Difference between struct and union in C++
{{Data types Data types Composite data types C (programming language) Articles with example C code de:Verbund (Datentyp)#Unions