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
32
votes
18 answers

C: Where is union practically used?

I have a example with me where in which the alignment of a type is guaranteed, union max_align . I am looking for a even simpler example in which union is used practically, to explain my friend.
yesraaj
  • 46,370
  • 69
  • 194
  • 251
32
votes
2 answers

What is the use of a constant union object?

If I make a const union object (e.g in code below ), then no member assignment can be done in that. So is there any use of making a const union object, in any case ? union un { int i; float f; char c; }; const union un a; /// ! a.i =…
cirronimbo
  • 909
  • 9
  • 19
30
votes
2 answers

What is idiomatic modern C++ for algebraic data types?

Suppose, for example, you want to implement a spreadsheet Cell in C++. A cell can be either a string, a number, or perhaps empty. Ignore other cases, like it being a formula. In Haskell, you might do something like: data Cell = CellStr String |…
blippy
  • 1,490
  • 1
  • 13
  • 22
29
votes
1 answer

Union element alignment

If I have a union, C standard guarantees that the union itself will be aligned to the size of the largest element. union U { long l; int i; short s; char c[2]; } u; But what does it say about alignment of individual union elements…
Alex B
  • 82,554
  • 44
  • 203
  • 280
29
votes
1 answer

Why is there a dummy union member in some implementations of std::optional?

Both libstdc++ (GNU) and libc++ (LLVM) implement std::optional value storage using a union and both of them include a dummy member. GNU implementation: using _Stored_type = remove_const_t<_Tp>; struct _Empty_byte { }; union { _Empty_byte…
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
29
votes
3 answers

Can union be templated?

It seems unions can be templated in c++11, they are used for example in the reference implementation of std::optional. Was that possible before c++11 ?
Drax
  • 12,682
  • 7
  • 45
  • 85
29
votes
2 answers

C++11 anonymous union with non-trivial members

I'm updating a struct of mine and I was wanting to add a std::string member to it. The original struct looks like this: struct Value { uint64_t lastUpdated; union { uint64_t ui; int64_t i; float f; bool b; }; }; Just adding…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
28
votes
2 answers

union 'punning' structs w/ "common initial sequence": Why does C (99+), but not C++, stipulate a 'visible declaration of the union type'?

Background Discussions on the mostly un-or-implementation-defined nature of type-punning via a union typically quote the following bits, here via @ecatmur ( https://stackoverflow.com/a/31557852/2757035 ), on an exemption for standard-layout structs…
underscore_d
  • 6,309
  • 3
  • 38
  • 64
27
votes
4 answers

memcpy/memmove to a union member, does this set the 'active' member?

Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy, it copies from the address of a plain old uint32_t, which is not contained within a union. Also, I am copying (via memcpy) to a…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
27
votes
5 answers

Initialization of a union in C

I came across this objective question on the C programming language. The output for the following code is supposed to be 0 2, but I don't understand why. Please explain the initialization process. Here's the code: #include int main() { …
Sumit Cornelius
  • 259
  • 3
  • 12
27
votes
6 answers

How to use c union nested in struct with no name

I'm working on the so called Hotspot open source project, and looking at the implementation I found a nasty nested union in struct looking like that: typedef struct RC_model_t_st { union { struct block_model_t_st *block; …
zwx
  • 315
  • 1
  • 4
  • 7
26
votes
2 answers

Memory layout of union of different sized member?

typedef union epoll_data { void *ptr; int fd; __uint32_t u32; __uint64_t u64; } epoll_data_t; Here int and __uint32_t are 4 bytes,while the others are 8 bytes. When we set fd to an int,does it lie on the first 4 bytes or…
cpuer
  • 7,413
  • 14
  • 35
  • 39
26
votes
7 answers

What's the major difference between "union" and "struct" in C.?

Possible Duplicate: Difference between a Structure and a Union in C I could understand what a struct means. But, i am bit confused with the difference between union and struct. Union is like a share of memory. What exactly it means.?
Vinay
  • 432
  • 1
  • 4
  • 13
24
votes
3 answers

get dictionary/object keys as tuple in typescript

I would like to get proper tuple type with proper type literals from an object in TS 3.1: interface Person { name: string, age: number } // $ExpectType ['name','age'] type ObjectKeysTuple = ToTuple Why?: To get proper string…
hotell
  • 528
  • 1
  • 5
  • 16
24
votes
4 answers

Narrowing string to string literal union

I want to narrow a string to a string literal union. In other words, I want to check if the string is one of the possible values of my literal union, so that this will work (if the operator couldbe existed). type lit = "A" | "B" | "C"; let uni:…
iFreilicht
  • 13,271
  • 9
  • 43
  • 74