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
11
votes
5 answers

Why are global anonymous unions required to be declared as static?

C++ 0x draft 9.5.6 Anonymous unions declared in a named namespace or in the global namespace shall be declared static. Why is this?
Vatsan
  • 1,163
  • 9
  • 15
11
votes
3 answers

Shapeless: map from coproduct to different coproduct

In the following, I'm trying to make a polymorphic function to convert a RawFeatureValue into a RefinedFeatureValue. import shapeless._ object test { type RawFeatureValue = Int :+: Double :+: String :+: CNil type RefinedFeatureValue = Int :+:…
Utaal
  • 8,484
  • 4
  • 28
  • 37
11
votes
4 answers

Pthreads and opaque types

I was reading the header files of the pthreads library and found this particular definition of the mutex (and other types) in bits/pthreadtypes.h: typedef union { struct __pthread_mutex_s { int __lock; unsigned int __count; int…
AnilM3
  • 261
  • 1
  • 9
11
votes
4 answers

What is the difference between a proper defined union and a reinterpret_cast?

Can you propose at least 1 scenario where there is a substantial difference between union { T var_1; U var_2; } and var_2 = reinterpret_cast (var_1) ? The more i think about this, the more they look like the same thing to me, at least from a…
user2485710
  • 9,451
  • 13
  • 58
  • 102
11
votes
2 answers

Padding in union is present or not

Hello all, I want to know whether union uses padding? since the size of union is the largest data member size, can there be padding at the end?
akash
  • 1,801
  • 7
  • 24
  • 42
11
votes
2 answers

C++: Strict aliasing vs union abuse

Apologies in advance for what may be a silly first post on well-trodden ground. While there is plenty of material on the subject, very little of it is definitive and/or intelligible to me. I have an AlignedArray template class to dynamically…
linguamachina
  • 5,785
  • 1
  • 22
  • 22
11
votes
2 answers

Implementation of C++ Cast

I was going through some code in CodeProject and came across the following code for C++ casting. template union horrible_union{ OutputClass out; InputClass in; }; template
KodeWarrior
  • 3,538
  • 3
  • 26
  • 40
11
votes
8 answers

Strange C++ boolean casting behaviour (true!=true)

Just read on an internal university thread: #include using namespace std; union zt { bool b; int i; }; int main() { zt w; bool a,b; a=1; b=2; cerr<<(bool)2<(2)<
SztupY
  • 10,291
  • 8
  • 64
  • 87
11
votes
1 answer

Union tested for current member in use

Do unions have a control structure to test which member is currently in use (or if it has any at all)? I'm asking this because undefined behavior is never a good thing to have in your program.
Alex D.
  • 427
  • 1
  • 5
  • 14
10
votes
1 answer

Active member of an union after assignment

Suppose sizeof( int ) == sizeof( float ), and I have the following code snippet: union U{ int i; float f; }; U u1, u2; u1.i = 1; //i is the active member of u1 u2.f = 1.0f; //f is the active member of u2 u1 = u2; My questions: Does it…
cerveka2
  • 156
  • 7
10
votes
6 answers

Unions versus structures in C

The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is-- let's say there is a structure struct strt { float f; char c; int a; } and the…
AGeek
  • 5,165
  • 16
  • 56
  • 72
10
votes
1 answer

Why does a type hint `float` accept `int` while it is not even a subclass?

On the one hand, I have learned that numbers that can be int or float should be type annotated as float (sources: PEP 484 Type Hints and this stackoverflow question): def add(a: float, b: float): return a + b On the other hand, an int is not an…
matheburg
  • 2,097
  • 1
  • 19
  • 46
10
votes
4 answers

Union not reinterpreting values?

Consider this program: #include union myUnion { int x; long double y; }; int main() { union myUnion a; a.x = 5; a.y = 3.2; printf("%d\n%.2Lf", a.x, a.y); return 0; } Output: -858993459 3.20 This is fine, as…
DarkAtom
  • 2,589
  • 1
  • 11
  • 27
10
votes
2 answers

How could one copy union simple members with memcpy?

I'm not quite sure about standard quotes about memcpy and union trivial members. Consider the code: struct Test{ union { void(*function_p)(void*); void(*function_p_c)(const void*); }; Test(const Test &other) { …
Alexander G.
  • 457
  • 2
  • 14
10
votes
5 answers

Is the following C union access pattern undefined behavior?

The following is not undefined behavior in modern C: union foo { int i; float f; }; union foo bar; bar.f = 1.0f; printf("%08x\n", bar.i); and prints the hex representation of 1.0f. However the following is undefined behavior: int…
dgnuff
  • 3,195
  • 2
  • 18
  • 32