unions Questions

17

Solved

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is the...
Deeprooted asked 6/12, 2008 at 17:56

2

The spec (GCC type attributes amongst others) states that __attribute__((packed)) can be applied to a struct or a union. Since all union fields' storage overlap, what does the attribute actually do...
Finedrawn asked 10/3, 2022 at 12:46

1

Solved

Suppose I have a struct that contains a union with const members, like so: struct S { // Members const enum { NUM, STR } type; union { const int a; const std::string s; }; // Constructo...
Kaenel asked 12/2, 2019 at 20:21

4

Solved

I found one code implemented as the similar demo shown below .. struct st { int a; struct { int b; }; }; 6.58 Unnamed struct/union fields within structs/unions As permitted by ISO C11. B...
Orgel asked 14/11, 2012 at 9:54

3

Solved

C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active ...
Mutinous asked 4/11, 2013 at 8:42

3

Solved

I have used bit field with a structure like this, struct { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; Now i wondered to see if this can be done wi...
Glob asked 4/7, 2012 at 9:41

4

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 tho...
Typhoeus asked 13/9, 2021 at 13:41

1

Solved

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 activ...
Marlea asked 22/8, 2021 at 12:40

2

Solved

I am currently working on programming a pool allocator. My question boils down to the following code: template <typename T> union myUnion { T data; myUnion<T>* nextUnion; }; void som...
Trilley asked 2/5, 2017 at 21:24

5

Solved

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 vi...
Flashing asked 13/1, 2012 at 4:24

1

Solved

I want to represent a 32-bit number using RGBA values, is it portable to generate the values for said number using a union? Consider this C code; union pixel { uint32_t value; uint8_t RGBA[4]; };...
Aquiculture asked 22/4, 2021 at 3:49

4

Solved

I'm writing some library code that exposes a const pointer to users but during certain operations I need to change where this pointer points (behind the scenes switcheroo tricks). One idea I had to...
Vanny asked 3/3, 2021 at 14:30

3

Solved

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 whi...
Ingeringersoll asked 10/10, 2014 at 10:21

3

Solved

Is there a better way to build a move constructor for a union-like class? If I were to have a union-like class like the class in the following code, is there a way to build the class or the move co...
Weinman asked 12/3, 2015 at 0:57

8

Solved

There's no way to do something like this, in C++ is there? union { { Scalar x, y; } Scalar v[2]; }; Where x == v[0] and y == v[1]?
Leeuwarden asked 31/3, 2009 at 19:25

2

Solved

For a project I'm working on, it's desirable to have a generic "pointer to a function" type. However, in C, to have a pointer to a function, you need to specify the prototype in the type ...
Donley asked 19/11, 2020 at 20:41

1

union data { double number2; char name[20]; }; int main() { printf("%i\n", sizeof(union data)); return 0; } I expected it to be 20 because it is the largest, but the result ...
Anderlecht asked 1/11, 2020 at 17:38

3

Solved

Whenever I see examples of union, they are always different types. For example, from MSDN: // declaring_a_union.cpp union DATATYPE // Declare union type { char ch; int i; long l; float f; dou...
Boutique asked 28/6, 2011 at 20:31

16

Solved

I have used unions earlier comfortably; today I was alarmed when I read this post and came to know that this code union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; ...
Monmouthshire asked 22/2, 2010 at 11:17

1

I saw such union inside struct definition in pure c code in Linux kernel sources struct cma_multicast (it's not the only one place. Seems that it is some common practice): struct cma_multicast { s...
Regulable asked 3/8, 2020 at 12:30

2

Solved

Does C++ guarantee that offsetof(U, m) always returns zero for any union U and its member m or are there cases when it's non-zero? union U { M m; ... };
Blankly asked 31/7, 2020 at 22:44

1

Solved

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): ...
Scholasticate asked 4/7, 2020 at 5:54

2

Solved

I'm attempting to implement a tagged union. My understanding was that in a C++ union, the non-trivial (i.e. not empty) destructors of non-static members are never called, thus we have to call them ...
Wheels asked 24/6, 2020 at 12:45

4

I guess this must have been asked before, but I could not get a specific yes/no answer. I have this code snippet : union integer_to_byte { signed int IntPart; unsigned char BytePart[2]; }; typ...
Munafo asked 24/3, 2020 at 14:31

12

Solved

I recently came across a great data structures book,"Data Structures Using C" (c) 1991, at a local Library book sale for only $2. As the book's title implies, the book covers data structures using ...
Landgraviate asked 13/5, 2009 at 13:47

© 2022 - 2024 — McMap. All rights reserved.