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

Does the C++17 standard guarantee that the address of a union is the same as the address of its members?

I am currently working on programming a pool allocator. My question boils down to the following code: template union myUnion { T data; myUnion* nextUnion; }; void someFunction(){ myUnion mu; T* t = new…
Brotcrunsher
  • 1,964
  • 10
  • 32
10
votes
2 answers

Where in the Standard does it say that the default member initializer for U::j should be ignored by the compiler?

Consider the following snippet: #include union U{ U(): i(1) {} int i; int j = 2; // this default member initializer is ignored by the compiler }; U u; int main(){ std::cout << u.i << '\n'; std::cout << u.j <<…
Ayrosa
  • 3,385
  • 1
  • 18
  • 29
10
votes
3 answers

How to use a union of two types

I am trying to make a vector that can hold string and int. I've tried the code below, but I get the compilation error error: use of deleted function 'my_union::~my_union()' What am I doing wrong? #include #include using…
cpp beginner
  • 512
  • 6
  • 23
10
votes
3 answers

Clarification on an example of unions in C11 standard

The following example is given in the C11 standard, 6.5.2.3 The following is not a valid fragment (because the union type is not visible within function f): struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *p1, struct t2 *p2) { if…
Kyle
  • 878
  • 6
  • 14
10
votes
1 answer

Thrift java client cannot handle union properly

Dead simple thrift union example. Env: latest thrift, cpp as server, java as client mytest.thrift: namespace java com.wilbeibi.thrift union Value { 1: i16 i16_v, 2: string str_v, } struct Box { 1: Value value; } service MyTest { Box…
wilbeibi
  • 3,403
  • 4
  • 25
  • 44
10
votes
2 answers

Do unrestricted unions require placement new and a constructor definition?

The examples I've seen of unrestricted unions always seem to use placement new when constructing. The Wikipedia article for C++11 features uses placement new in the constructor of a…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
10
votes
1 answer

union of structs sharing same first members

I have been looking into an un-traditional way of achieving struct "polymorphism" in pre-C11 C. Let's say we have 2 structs: struct s1 { int var1; char var2; long var3; }; struct s2 { int var1; char var2; long var3; char…
user3079266
10
votes
2 answers

How to cast C struct just another struct type if their memory size are equal?

I have 2 matrix structs means equal data but have different form like these: // Matrix type 1. typedef float Scalar; typedef struct { Scalar e[4]; } Vector; typedef struct { Vector e[4]; } Matrix; // Matrix type 2 (you may know this if you're…
eonil
  • 83,476
  • 81
  • 317
  • 516
10
votes
3 answers

How to know which variable value is set for union?

I am working on optimization of a project. It contains a struct of an options in which user can select a single option at a time. In addition to the option, we also use a flag variable to check which option value is set for this record. In…
Fadie Filat
  • 105
  • 1
  • 6
10
votes
1 answer

Is this a GCC bug? Initializing structs with unions

I may have found a bug with GCC v4.8.2, but I want to check first before I submit it as it could be me doing something wrong! The following code: #include struct Message { typedef union { char byte; const char *str; }…
Sam Kellett
  • 1,277
  • 12
  • 33
10
votes
3 answers

Strange Behaviour Class Objects Inside Union

Hi I wanted know the reasons of the following code void main() { class test { public: test(){} int k; }; class test1 { public: test1(){} int k; }; union Test { test t1; test1 t2; …
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75
10
votes
1 answer

Templated unions in c++11

Does the c++11 standard say anything about templated unions? (I can't find anything in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, but I haven't read it that carefully.) I have template union u { T a; char…
Jason Gross
  • 5,928
  • 1
  • 26
  • 53
10
votes
3 answers

How can I simulate a C++ union in C#?

I have a small question about structures with the LayoutKind.Explicit attribute set. I declared the struct as you can see, with a fieldTotal with 64 bits, being fieldFirst the first 32 bytes and fieldSecond the last 32 bytes. After setting both…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
9
votes
5 answers

How can I prevent a nameless struct\union?

I am building a class that has a union for its matrix data, however, I can only get it compile when I do not have a name for the struct\union. However, with a higher level warning level (four on visual studio) I will a warning saying warning C4201:…
mmurphy
  • 1,327
  • 4
  • 15
  • 30
9
votes
0 answers

Real life example of C UNIONS

Possible Duplicate: Unions in C I have been asked in an interview to give a real life example of unions. I suggested him unions can be used in low level system programming.He asked me give an example related to it. I told we can use union to find…
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199