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
7
votes
1 answer

C overcoming aliasing restrictions (unions?)

Assume I have a sample source file, test.c, which I am compiling like so: $ gcc -03 -Wall test.c looks something like this .. /// CMP128(x, y) // // arguments // x - any pointer to an 128-bit int // y - any pointer to an 128-bit int // //…
Todd Freed
  • 877
  • 6
  • 19
7
votes
4 answers

union initialisation

I'm attempting to globally initialise a union as in the following example: #include typedef union { char t[4]; int i; } a; enum { w = 5000, x, y, z }; a temp = {w}; int main() { printf("%d %d %d %d %d\n",…
Ben Stott
  • 2,218
  • 17
  • 23
7
votes
4 answers

Is a union more efficient than a shift on modern compilers?

Consider the simple code: UINT64 result; UINT32 high, low; ... result = ((UINT64)high << 32) | (UINT64)low; Do modern compilers turn that into a real barrel shift on high, or optimize it to a simple copy to the right location? If not, then using a…
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
7
votes
1 answer

TypeScript Discriminated Union Type with default and type inference

I want to create a Discriminated Union Type, where it isn't required to pass the discriminator value. Here's my current code: interface Single { multiple?: false // this is optional, because it should be the default value: T onValueChange:…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
7
votes
1 answer

Handle errors with union

I came to C from high-level Scala language and came up with a question. In Scala we usually handle error/exceptional conditions using Either which looks like the following: sealed abstract class Either[+A, +B] extends Product with Serializable So…
Some Name
  • 8,555
  • 5
  • 27
  • 77
7
votes
1 answer

How to set union in class initializer?

Given a class such as the one below and the given union, how does one initialize the union to the correct value? What is being attempted here is to use two or more different types as one of the core data types for the class. Instead of using…
glenng
  • 161
  • 1
  • 5
7
votes
1 answer

Implementing a move constructor of a tagged union

I implemented a tagged union using a class containing an anonymous union and a tag: class LogFile { public: LogFile(std::ostream& stdStream); LogFile(std::ofstream fileStream); LogFile(LogFile&& logFile); ~LogFile(); …
timakro
  • 1,739
  • 1
  • 15
  • 31
7
votes
6 answers

Unions that contain a"type" member

I have a question about something I still don't understand about unions. I've read about a lot of their uses and for the most part can see how they can be useful and understand them. I've seen that they can provide a primitive "C style"…
Russel
  • 2,335
  • 3
  • 19
  • 11
7
votes
1 answer

Access fields in a named union

I want to have a named union in the following struct so that I can memcpy it without knowing what field is "active". struct Literal { enum class Type : size_t { INT = 1, LONG, FLOAT, DOUBLE } type; union { int li; long…
dblouis
  • 568
  • 1
  • 5
  • 18
7
votes
2 answers

Rationale behind active members of unions

C++'s unions are more restrictive than those of C, because they introduce the concept of an "active member" (the one last assigned to) as the only one safe to access. The way I see it, this behavior of unions is a net negative. Can someone please…
debiatan
  • 111
  • 4
7
votes
3 answers

Does union support flexible array members?

I have declared a flexible array member in union, like this: #include union ut { int i; int a[]; // flexible array member }; int main(void) { union ut s; return 0; } and compiler gives an error : source_file.c:8:9:…
msc
  • 33,420
  • 29
  • 119
  • 214
7
votes
1 answer

Writing to Unions, with gcc

Consider the following types: struct A { int x; }; struct B { int y; char z; }; union U { A a; B b; }; And this code fragment: U u; new (&u.b) B; b.y = 42; b.z = 'x'; At this point, reading from u.a.x is well-defined behavior (and will yield 42)…
Barry
  • 286,269
  • 29
  • 621
  • 977
7
votes
2 answers

Shared ptr in union

I want to access shared ptr, which is in union, though segmentation fault happens: struct union_tmp { union_tmp() {} ~union_tmp() {} union { int a; std::shared_ptr> ptr; }; }; int…
Aleksandr Tukallo
  • 1,299
  • 17
  • 22
7
votes
5 answers

Unions within unions

In C, is it possible to define a union within another union? If no, why is it not possible? Or if yes, where can it be used?
Raj
  • 4,342
  • 9
  • 40
  • 45
7
votes
4 answers

How to create a union with a 32 bit int and four 8 bit char types that each refer to difference slice of the 32 bit int?

I want to create a union in which the biggest member is a 32 bit integer. This is what will be mainly written to. Then there are four 8 bit variables, probably char types that will each refer to a different section of the 32 bit integer kind of…
quantum231
  • 2,420
  • 3
  • 31
  • 53