unions

Accessing same-type inactive member in unions

隐身守侯 提交于 2019-11-27 02:46:00
问题 I have something like this: union DataXYZ { struct complex_t { float real, imag; } complex; struct vector_t { float magnitude, phase; } vector; }; I have some vectors of these, being general-purpose workspace memory, where I use the fields accordingly after the semantic context. I know it's undefined behaviour to read a field in a union, when the last active member was another field (and type?). Does this matter when the types and layout match exactly? I've been commenting on some other

Can a union be initialized in the declaration?

六月ゝ 毕业季﹏ 提交于 2019-11-27 01:42:30
For example, say we have a union typedef union { unsigned long U32; float f; }U_U32_F; When a variable of this union type is declared, is there a way to set an initial value? U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax for this? Use an initializer list: U_U32_F u = { 0xffffffff }; You can set other members than the first one via U_U32_F u = { .f = 42.0 }; Try U_U32_F u = {0xffffffff}; Note that per-member union initialization doesn't work on pre-C99 compilers, of which there is a depressing number out there. The current Microsoft C compiler doesn't support it, for

gcc, strict-aliasing, and casting through a union

China☆狼群 提交于 2019-11-26 22:03:24
Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union: [...] Taking the address, casting the resulting pointer and dereferencing the result has undefined behavior [emphasis added], even if the cast uses a union type, e.g.: union a_union { int i; double d; }; int f() { double d = 3.0; return ((union a_union *)&d)->i; } Does anyone have an example to illustrate this undefined behavior? Note this question is not about what the C99 standard says, or does not say. It is about the actual functioning of gcc ,

C++ unions vs. reinterpret_cast

我的梦境 提交于 2019-11-26 21:11:45
问题 It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the integer value of 0xffff and literally interpret it as a series of bits in IEEE 754 floating point. (Binary convert shows visually how this is done.) #include <iostream> using namespace std; union unionType { int myInt; float myFloat; }; int main() { int

strict aliasing and alignment

余生颓废 提交于 2019-11-26 19:37:06
问题 I need a safe way to alias between arbitrary POD types, conforming to ISO-C++11 explicitly considering 3.10/10 and 3.11 of n3242 or later. There are a lot of questions about strict aliasing here, most of them regarding C and not C++. I found a "solution" for C which uses unions, probably using this section union type that includes one of the aforementioned types among its elements or nonstatic data members From that I built this. #include <iostream> template <typename T, typename U> T& access

How do boost::variant and boost::any work?

会有一股神秘感。 提交于 2019-11-26 18:51:26
问题 How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded assignment operators. I queried the size of any and variant, and did some experiments with them. In my platform, variant takes the size of its longest possible type plus 8 bytes: I think it my just be 8 bytes o type information and the rest being

When would anyone use a union? Is it a remnant from the C-only days?

こ雲淡風輕ζ 提交于 2019-11-26 18:08:53
I have learned but don't really get unions. Every C or C++ text I go through introduces them (sometimes in passing), but they tend to give very few practical examples of why or where to use them. When would unions be useful in a modern (or even legacy) case? My only two guess would be programming microprocessors when you have very limited space to work with, or when you're developing an API (or something similar) and you want to force the end user to have only one instance of several objects/types at one time. Are these two guesses even close to right? vz0 Unions are usually used with the

Examples of Union in C [closed]

心不动则不痛 提交于 2019-11-26 15:32:39
问题 I'm looking for some union examples, not to understand how union works, hopefully I do, but to see which kind of hack people do with union. So feel free to share your union hack (with some explanation of course :) ) 回答1: One classic is to represent a value of "unknown" type, as in the core of a simplistic virtual machine: typedef enum { INTEGER, STRING, REAL, POINTER } Type; typedef struct { Type type; union { int integer; char *string; float real; void *pointer; } x; } Value; Using this you

How to compile C code with anonymous structs / unions?

北慕城南 提交于 2019-11-26 15:07:17
I can do this in c++/g++: struct vec3 { union { struct { float x, y, z; }; float xyz[3]; }; }; Then, vec3 v; assert(&v.xyz[0] == &v.x); assert(&v.xyz[1] == &v.y); assert(&v.xyz[2] == &v.z); will work. How does one do this in c with gcc? I have typedef struct { union { struct { float x, y, z; }; float xyz[3]; }; } Vector3; But I get errors all around, specifically line 5: warning: declaration does not declare anything line 7: warning: declaration does not declare anything user287561 according to http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields -fms-extensions will enable the

Union element alignment

吃可爱长大的小学妹 提交于 2019-11-26 14:44:36
问题 If I have a union, C standard guarantees that the union itself will be aligned to the size of the largest element. union U { long l; int i; short s; char c[2]; } u; But what does it say about alignment of individual union elements inside the union? Is the following expression guaranteed to be true? (&u.l == &u.i) && (&u.i == &u.s) && (&u.s == &u.c[0]) 回答1: The start of each element is aligned with the address of the union itself. so the individual comparisons in the expression you ask about