Questions tagged [unions]

The plural of a keyword in the C family of languages for declaring a union data type.

A union is a single variable with multiple possible representations. For example:

union foo {
    int a;
    float b;
    struct {
        char c, d;
    } e;
};

is a variable foo that can be accessed as either an int, float, or struct. Although similar in format to a struct, the size of a union is equal to the size of the largest field where as the size of a structure is the sum of the sizes of all fields.

They can also be used for a primitive form of polymorphism. A program can check for types and use different fields in a union in different contexts.


For the SQL UNION keyword, use instead of this tag.

1638 questions
19
votes
1 answer

Constructor and copy-constructor for class containing union with non-trivial members

I am trying to implement a custom variant type which uses a union to store data of various different types. In the field type_id I plan to store which type the data stored in the union is of. The union contains non-trivial members. Here is my…
j00hi
  • 5,420
  • 3
  • 45
  • 82
19
votes
3 answers

Is it valid to use bit fields with union?

I have used bit field with a structure like this, struct { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; Now i wondered to see if this can be done with a union so i modified the code…
amin__
  • 1,018
  • 3
  • 15
  • 22
18
votes
2 answers

Zero-cost properties with data member syntax

I have (re?)invented this approach to zero-cost properties with data member syntax. By this I mean that the user can write: some_struct.some_member = var; var = some_struct.some_member; and these member accesses redirect to member functions with…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
18
votes
4 answers

Best practice for unions in Go

Go has no unions. But unions are necessary in many places. XML makes excessive use of unions or choice types. I tried to find out, which is the preferred way to work around the missing unions. As an example I tried to write Go code for the non…
ceving
  • 21,900
  • 13
  • 104
  • 178
17
votes
4 answers

Is const-casting via a union undefined behaviour?

Unlike C++, C has no notion of a const_cast. That is, there is no valid way to convert a const-qualified pointer to an unqualified pointer: void const * p; void * q = p; // not good First off: Is this cast actually undefined behaviour? In any…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
17
votes
6 answers

Is there a way to access individual bits with a union?

I am writing a C program. I want a variable that I can access as a char but I can also access the specific bits of. I was thinking I could use a union like this... typedef union { unsigned char status; bit bits[8]; }DeviceStatus; but the…
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
17
votes
2 answers

Is it legal and well defined behavior to use a union for conversion between two structs with a common initial sequence (see example)?

I have an API with a publicly facing struct A and an internal struct B and need to be able to convert a struct B into a struct A. Is the following code legal and well defined behavior in C99 (and VS 2010/C89) and C++03/C++11? If it is, please…
Coder
  • 441
  • 2
  • 17
16
votes
5 answers

Is using an union in place of a cast well defined?

I had a discussion this morning with a colleague regarding the correctness of a "coding trick" to detect endianness. The trick was: bool is_big_endian() { union { int i; char c[sizeof(int)]; } foo; foo.i = 1; return (foo.c[0] ==…
ereOn
  • 53,676
  • 39
  • 161
  • 238
16
votes
2 answers

std::optional implemented as union vs char[]/aligned_storage

While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template class optional { // ... private: bool has_value_; aligned_storage
Ron
  • 1,989
  • 2
  • 17
  • 33
16
votes
2 answers

How are the union members stored?

union test { int i; char ch; }t; int main() { t.ch=20; } Suppose sizeof(int)==2 and let the memory addresses allocated for t are 2000, 2001. Then where is 20 i.e. t.ch stored - at 2000 or 2001 or depends on endianness of machine?
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60
16
votes
6 answers

How to know that which variable from Union is Used?

If I declare a Union as: union TestUnion { struct { unsigned int Num; unsigned char Name[5]; }TestStruct; unsigned char Total[7]; }; Now, How can I know that whether Total[7] is used or TestStruct is used? I am using…
Swanand
  • 4,027
  • 10
  • 41
  • 69
16
votes
1 answer

Accessing same-type inactive member in unions

I have something like this: union DataXYZ { struct complex_t { float real, imag; } complex; struct vector_t { float magnitude, phase; } vector; }; I have some vectors of these, being general-purpose…
Shaggi
  • 1,121
  • 1
  • 9
  • 31
16
votes
4 answers

Is there an elegant way to swap references in C++?

Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of original instances instead of references. The code below illustrates this behavior: #include…
bkxp
  • 1,115
  • 1
  • 12
  • 20
16
votes
5 answers

Is it undefined behaviour to call a function with pointers to different elements of a union as arguments?

This code prints different values after compiling with -O1 and -O2 (both gcc and clang): #include static void check (int *h, long *k) { *h = 5; *k = 6; printf("%d\n", *h); } union MyU { long l; int i; }; int main (void) { …
Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
16
votes
11 answers

union versus void pointer

What would be the differences between using simply a void* as opposed to a union? Example: struct my_struct { short datatype; void *data; } struct my_struct { short datatype; union { char* c; int* i; long*…
user105033
  • 18,800
  • 19
  • 58
  • 69