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 Union Polymorphism within Arrays

Given the following: typedef struct{ ... ... } A; typedef struct{ ... ... } B; typedef union __attribute__((transparent_union)) { A a; B b; } C; If I declare a function myMethod(C){ ... } The following is legal without explicit…
AFS
  • 628
  • 1
  • 6
  • 13
6
votes
5 answers

Union and struct packing problem

I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important. typedef union{ uint32_t raw; struct{ unsigned int present:1; unsigned int rw:1; unsigned int user:1; unsigned int dirty:1; …
Earlz
  • 62,085
  • 98
  • 303
  • 499
6
votes
2 answers

Anonymous union/struct of class with constructor

GCC complains about this code even though I compile with -std=c++11 flag, and my gcc version supposedly supports Unrestricted unions (>4.6). union { struct { float4 I,J,K,T; }; struct { float4 m_lines[4]; …
qdii
  • 12,505
  • 10
  • 59
  • 116
6
votes
2 answers

C++ union to represent data memory vs C scalar variable type

Today I've a weird question. The Code(C++) #include union name { int num; float num2; }oblong; int main(void) { oblong.num2 = 27.881; std::cout << oblong.num << std::endl; return 0; } The Code(C) #include…
caramel1995
  • 2,968
  • 10
  • 44
  • 57
6
votes
4 answers

When to use a union and when to use a structure

I know the differences between union and structure. But from a design and coding perspective what are the various use cases of using a union instead of a structure? One is a space optimization. Are there any more advantages of using them?
karan
  • 313
  • 2
  • 3
  • 20
6
votes
1 answer

Aggregate initialization of a union in C++ with `{}`

In the following program the union U has two fields a and b, each with distinct default value. If one creates a variable of type U using aggregate initialization {} what are the value and the active member of the union? #include struct A…
Fedor
  • 17,146
  • 13
  • 40
  • 131
6
votes
0 answers

Undefined behavior of default constructor and destructor of anonymous union?

The following code gives different compilation result with g++ 7.3 and clang++ 7.0, but to my knowledge all of copy constructor of S1, S2, S3 is deleted and none of the instantiation should success. Question: Is such case (S1, S2) undefined in C++…
Midori Yakumo
  • 184
  • 12
6
votes
1 answer

What is the difference between initializing a struct vs anonymous struct?

Consider the following code: typedef struct { uint32_t a : 1; uint32_t b : 1; uint32_t c : 30; } Foo1; void Fun1(void) { volatile Foo1 foo = (Foo1) {.a = 1, .b = 1, .c = 1}; } This general pattern comes up quite a bit when using bit fields…
user2465116
  • 388
  • 2
  • 10
6
votes
0 answers

Is it Undefined Behavior to call virtual functions of union member with a common base class as another member?

Specifically, I would like to be able to use polymorphism without heap allocation in an embedded context (thus without dynamic allocation). My concern here seems to be that accessing the base member while x or y are "active" in the union seems to be…
gvl
  • 903
  • 7
  • 16
6
votes
1 answer

Is there any difference between the members of this C union?

The typedef below is for the DIR register from the Atmel SAMD21 ARM MCU include file. Since the bit struct member and the reg member are both 32 bits, is there any difference between the two members in the union? I'm trying to understand why they…
crj11
  • 197
  • 7
6
votes
1 answer

Inspecting non-active union members, common initial sequence

This question is based on this Consider the following: struct Hdr { int type; }; struct A { Hdr h; }; union Big { Hdr h; A a; }; and suppose that for Big big we know that big.a is the active member of the union. Is the access to big.h.type…
xeros
  • 123
  • 8
6
votes
5 answers

c++ casting a union to one of its member types

The following seems perfectly logical to me, but isn't valid c++. A union cannot be implicitly cast to one of it's member types. Anyone know a good reason why not? union u { int i; char c; } function f(int i) { } int main() { u v; v.i = 6; …
Baruch
  • 20,590
  • 28
  • 126
  • 201
6
votes
1 answer

Are pointers to members of an anonymous union equal?

C++11 allows an anonymous union to be defined in a function, and its members can be accessed as variables of the function. If I examine the pointers to the different members, they are the same, but the == operator says they're unequal. Such strange…
ugoren
  • 16,023
  • 3
  • 35
  • 65
6
votes
3 answers

Array of Union on C

I have declared this array of unions: union Function { char* name; double (*fnct)(); int args; }; union Function functions[] = { {.name = "acos", .fnct = acos, .args = 1}, {.name = "asin", .fnct = asin, .args = 1}, {.name =…
Pablo
  • 63
  • 1
  • 1
  • 3
6
votes
2 answers

Placement-new an STL container and destroying it safely afterwards

This code implements an unrestricted union which provides access by name and by index to any of its three members. Since std::string is non-trivially constructed and destroyed, I need to provide special constructor and destructor for the…
GetFree
  • 40,278
  • 18
  • 77
  • 104