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
8
votes
2 answers

C2280: attempting to reference a deleted function (union, struct, copy constructor)

I have a problem with misleading error messages, when I try to compile the following minimal sample in Visual Studio 2015: class Vector { float x; float y; public: Vector(float x, float y) : x(x), y(y) {} Vector& operator = (const…
Gustav-Gans
  • 365
  • 1
  • 3
  • 13
8
votes
2 answers

What kind of conversion is this code doing?

I ran across this code, where they are trying to convert from float to int int val[5]; union { int i; float f; } conv; ... val is updated with some value ... case OUT_FORMAT_FLOAT: for (i = 0; i < count; i++) { conv.f = val[i]; …
bluefalcon
  • 4,225
  • 1
  • 32
  • 41
8
votes
4 answers

C/C++ unions and undefined behaviour

Is the following undefined behaviour? union { int foo; float bar; } baz; baz.foo = 3.14 * baz.bar; I remember that writing and reading from the same underlying memory between two sequence points is UB, but I am not certain.
Vroomfondel
  • 2,704
  • 1
  • 15
  • 29
8
votes
2 answers

How to write destructor for union-like class

I'm trying to use an union (C++) that has some non-primitive variables, but I'm stuck trying to create the destructor for that class. As I have read, it is not possible to guess what variable of the union is being used so there is no implicit…
ranieri
  • 2,030
  • 2
  • 21
  • 39
8
votes
1 answer

Is reading inactive union member of the same type as active one well-defined?

Consider the following structure: struct vec4 { union{float x; float r; float s}; union{float y; float g; float t}; union{float z; float b; float p}; union{float w; float a; float q}; }; Something like this seems to be used in e.g.…
Ruslan
  • 18,162
  • 8
  • 67
  • 136
8
votes
2 answers

Overlay subclass union ontop of superclass union

I'm wondering if it's possible to append members to a C++ union in a subclass. class A { ... union { int a; int b; }; }; class B : public A { ... int c; //<- Can this use the same storage as the union? }; A more concrete…
Matthew G.
  • 1,298
  • 10
  • 24
8
votes
5 answers

Understanding the memory content of a union

Suppose I define a union like this: #include int main() { union u { int i; float f; }; union u tst; tst.f = 23.45; printf("%d\n", tst.i); return 0; } Can somebody tell me what the memory where…
Moeb
  • 10,527
  • 31
  • 84
  • 110
8
votes
5 answers

#define struct union and #define else still compiles any C program?

Someone claimed in a presentation that if you add #define struct union #define else at the beginning of any valid C program, that program would still compile. That seems like a bold claim. Any counterexample you have in mind and prove that guy…
webuster
  • 2,490
  • 18
  • 27
8
votes
1 answer

Is it possible (legal) to assign an anonymous union in a compound literal?

I have a struct: typedef struct _n { int type; union { char *s; int i; }; } n; When I try to assign a compound literal, like: node n1 = {1, 0}; node n2 = {2, "test"}; gcc gives me some warnings such as: warning:…
Steve
  • 1,849
  • 2
  • 19
  • 19
8
votes
2 answers

Getting Union, Intersection, or Difference of Sets in C++

I have a couple questions about how to use C++ sets (std::set) Is there a way to get the union, intersection, or difference of two C++ sets? (It's pretty easy to write my own functionto do that but I wanted to know if there was a built in function…
Alex319
  • 3,818
  • 9
  • 34
  • 40
8
votes
5 answers

What is the correct way to check equality between instances of a union?

I have a multithreaded application that stores data as an array of instances of the following union union unMember { float fData; unsigned int uiData; }; The object that stores this array knows what type the data in the union is and so I…
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
8
votes
1 answer

Why aligning of long long union member is bigger than the containing union/struct? Is this correct?

From this question one could start to believe that alignment of a union is not less than the largest alignment of it's individual members. But I have a problem with the long long type in gcc/g++. The full example can be found here, but here are the…
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
8
votes
2 answers

Two arrays in a union in C++

is it possible to share two arrays in a union like this: struct { union { float m_V[Height * Length]; float m_M[Height] [Length]; } m_U; }; Do these two arrays share the same memory size or is…
Tobias
  • 427
  • 4
  • 19
7
votes
4 answers

Can an Ada Variant Record be binary compatible to a C++ union?

I am designing a communication middleware for use in an application which has a module in Ada and many modules in C++ which communicates passing parameters (scalar values) and structures. The application runs in MS Windows XP and Windows 7, the C++…
Guarita
  • 173
  • 1
  • 11
7
votes
4 answers

Why is this UNION like a struct?

I came across some code (it's in a library from Microchip) which has a union. All was good, until I saw it assigned values to different members of the union right after each other. My immediate thought was "They are over writing the same…
SpacemanScott
  • 953
  • 9
  • 21