Union (computer science)

GPTKB entity

Statements (51)
Predicate Object
gptkbp:instanceOf Data structure
gptkbp:advantage Allows flexible data representation
Harder to debug
Not type safe
Potential for bugs
Saves memory
gptkbp:alsoKnownAs gptkb:Variant
Discriminated union
Tagged union
gptkbp:cause Type safety issues
gptkbp:contrastsWith gptkb:Struct_(computer_science)
gptkbp:definedIn gptkb:C++_standard
gptkb:C_standard
gptkbp:example union Data { int i; float f; char c; }
gptkbp:fieldAccess Only one field valid at a time
gptkbp:fieldAccessExample u.f = 3.14;
u.i = 10;
gptkbp:hasSyntax union { int i; float f; } u;
https://www.w3.org/2000/01/rdf-schema#label Union (computer science)
gptkbp:introducedIn gptkb:C_programming_language_(1972)
gptkbp:limitation Difficult to use safely in high-level code
No automatic type checking
Undefined behavior if wrong field accessed
gptkbp:memoryLayout All members share the same memory location
gptkbp:memoryUsage Size of largest member
gptkbp:numberOfLocations Different data types in the same memory location
gptkbp:purpose Efficient memory usage
gptkbp:relatedConcept gptkb:Algebraic_data_type
gptkb:Tagged_pointer
Option type
Sum type
Variant record
gptkbp:sizeDeterminedBy Largest member
gptkbp:usedFor Device drivers
Embedded systems
Interpreting same memory as different types
Low-level memory manipulation
Protocol implementations
Type punning
gptkbp:usedIn gptkb:Rust_programming_language
gptkb:C_programming_language
gptkb:C++_programming_language
gptkb:Ada_programming_language
gptkb:Pascal_programming_language
gptkb:Swift_programming_language
Low-level programming
Operating systems
Compilers
Systems programming
gptkbp:bfsParent gptkb:Union
gptkbp:bfsLayer 5