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
12
votes
4 answers

Why is sizeof(std::variant) the same size as a struct with the same members?

The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or it holds no value. sizeof(std::variant) == 16 But if it is…
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
12
votes
3 answers

How to memset an anonymous union with 0

How should I zero out an anonymous union? I couldn't find anything on cppreference page about it. Would memseting it's largest member with 0 work here? For example - #include #include struct s{ char a; char…
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
12
votes
2 answers

Do array elements count as a common initial sequence?

Sort of related to my previous question: Do elements of arrays count as a common initial sequence? struct arr4 { int arr[4]; }; struct arr2 { int arr[2]; }; union U { arr4 _arr4; arr2 _arr2; }; U u; u._arr4.arr[0] = 0; //write to…
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
12
votes
2 answers

Unions in C++11: default constructor seems to be deleted

I am trying to understand how unions were extended by C++11. One thing that changed is the ability to use now non-static data members with non-trivial special member functions. From cppreference.com If a union contains a non-static data member with…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
12
votes
3 answers

Nameless union inside a union

I'm reading some code and found something like the following: typedef union { int int32; int boolean; time_t date; char *string; union { struct foo *a; struct foo *b; struct foo *c; }; } type_t; From…
lang2
  • 11,433
  • 18
  • 83
  • 133
12
votes
3 answers

Error: Redefinition of union

In the following code: typedef struct { union U { int a; char b; }U1; }A; typedef struct { union U { int a; char b; }U1; }B; The compiler gives an error…
Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35
12
votes
3 answers

What does C standard say about a union of two identical types

Is it possible to to have the following assert fail with any compiler on any architecture? union { int x; int y; } u; u.x = 19; assert(u.x == u.y);
perreal
  • 94,503
  • 21
  • 155
  • 181
12
votes
3 answers

Accessing C union members via pointers

Does accessing union members via a pointer, as in the example below, result in undefined behavior in C99? The intent seems clear enough, but I know that there are some restrictions regarding aliasing and unions. union { int i; char c; } u; int …
Dara Hazeghi
  • 123
  • 1
  • 1
  • 5
12
votes
4 answers

What are the benefits of unnamed structs / unions in C?

I found one code implemented as the similar demo shown below .. struct st { int a; struct { int b; }; }; 6.58 Unnamed struct/union fields within structs/unions As permitted by ISO C11. But What are benefits of it ? Because anyway I can access…
Omkant
  • 9,018
  • 8
  • 39
  • 59
11
votes
6 answers

Is it possible to put several objects together inside a union?

What if I have this: union{ vector intVec ; vector floatVec ; vector doubleVec ; } ; Of course, I'll be using just one of the 3 vectors. But... what happens when all the 3 vectors are contructed?? Would the…
GetFree
  • 40,278
  • 18
  • 77
  • 104
11
votes
4 answers

Naming Array Elements, or Struct And Array Within a Union

Consider the following struct: struct Vector4D { union { double components[4]; struct { double x, y, z, t; } Endpoint; }; }; It seems to me that I have seen something similar in WinApi's IPAddress struct. The idea is to give me…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
11
votes
1 answer

Using std::launder to get a pointer to an active object member from a pointer to an inactive object?

This question followes this one Let's consider this example code: struct sso { union{ struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; bool is_short_str() const{ return…
Oliv
  • 17,610
  • 1
  • 29
  • 72
11
votes
3 answers

Are there any guarantees for unions that contain a wrapped type and the type itself?

Can I put a T and a wrapped T in an union and inspect them as I like? union Example { T value; struct Wrapped { T wrapped; } wrapper; }; // for simplicity T = int Example ex; ex.value = 12; cout << ex.wrapper.wrapped; // ? The…
Zeta
  • 103,620
  • 13
  • 194
  • 236
11
votes
3 answers

Is it safe to detect endianess with union?

In other words, according to the C standard, is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little…
iBug
  • 35,554
  • 7
  • 89
  • 134
11
votes
2 answers

C++11 empty list Initialization of a union - is it guaranteed to initialize the full length of the union?

In C++11, I have the following union: union SomeData { std::uint8_t Byte; std::uint16_t Word; std::uint32_t DWord; unsigned char String[128]; }; If I initialize the union thusly; SomeData data {}; Is it guaranteed that the entire…
BTownTKD
  • 7,911
  • 2
  • 31
  • 47