unions

Union tested for current member in use

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:17:33
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. No, no such mechanism exists off-the-shelf. You'll have to take care of that yourself. The usual approach is wrapping the union in a struct : struct MyUnion { int whichMember; union { //whatever } actualUnion; }; So you have MyUnion x; and x.whichMember tells you which field of x.actualUnion is in use (you have to implement the functionality though). 来源: https://stackoverflow.com/questions/11035534/union

When to use a union and when to use a structure

爱⌒轻易说出口 提交于 2019-11-30 15:19:57
I know the differences between union and structure. But from a design and coding perspective what are the various use cases of using a union instead of a structure? One is a space optimization. Are there any more advantages of using them? There's really only two major uses. The first is to create a discriminated union. That's probably what you were thinking of by "space optimization," but there's a bit more to it. You need an extra bit of data to know which member of the union is "alive" (has valid data in it) since the compiler does not do this for you. You'd usually see code like that having

Union and struct packing problem

无人久伴 提交于 2019-11-30 14:35:15
I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important. typedef union{ uint32_t raw; struct{ unsigned int present:1; unsigned int rw:1; unsigned int user:1; unsigned int dirty:1; unsigned int free:7; unsigned int frame:20; } __packed; }__packed page_union_t; that is my structure and union. It does not work however: page_union_t p; //..... //This: p.frame=trg_page; p.user=user; p.rw=rw; p.present=present; //and this: p.raw=trg_page<<12 | user<<2 | rw<<1 | present; should create the same uint32. But they do not create the same thing. Is there

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

青春壹個敷衍的年華 提交于 2019-11-30 12:39:44
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 iPhone developer) // Defines CGFloat as float for simple description. typedef float CGFloat; struct CATransform3D { CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m24; CGFloat m31, m32, m33, m34; CGFloat m41, m42, m43, m44; }; typedef struct CATransform3D CATransform3D; Their memory sizes are equal. So I believe there is a way to convert these types

Constructor and copy-constructor for class containing union with non-trivial members

☆樱花仙子☆ 提交于 2019-11-30 12:22:55
I am trying to implement a custom variant type which uses a union to store data of various different types. In the field type_id I plan to store which type the data stored in the union is of. The union contains non-trivial members. Here is my current implementation: struct MyVariant { enum { t_invalid, t_string, t_int, t_double, t_ptr, t_dictionary } type_id; union { int as_int; double as_double; std::string as_string; std::unique_ptr<int> as_ptr; std::map<int, double> as_dictionary; }; }; I try to create an instance of MyVariant like follows: MyVariant v; I get the error message: Call to

Accessing C union members via pointers

夙愿已清 提交于 2019-11-30 12:15:20
Does accessing union members via a pointer, as in the example below, result in undefined behavior in C99? The intent seems clear enough, but I know that there are some restrictions regarding aliasing and unions. union { int i; char c; } u; int *ip = &u.i; char *ic = &u.c; *ip = 0; *ic = 'a'; printf("%c\n", u.c); It is unspecified (subtly different from undefined) behaviour to access a union by any element other than the one that was last written. That's detailed in C99 annex J: The following are unspecified: : The value of a union member other than the last one stored into (6.2.6.1). However,

Union vs. static_cast(void*)

丶灬走出姿态 提交于 2019-11-30 09:31:25
问题 I'm writing code and until now I was using structures like this: struct s{ enum Types { zero = 0, one, two }; unsigned int type; void* data; } I needed some generic structure to store data from different classes and I wanted to use it in std::vector, so that's reason why I can't use templates. What's better option: unions or void pointers? Void pointer allocates only as much space as I need, but c++ is strong typed language for some reason and casting everywhere I need to use those data is

Union of structs with common first member

爱⌒轻易说出口 提交于 2019-11-30 08:44:34
问题 I am unsure of whether or not the code has pointer aliasing (or other standard conformance issues) in the asserts cast. It seems that a pointer to the union type should be able to be cast to a pointer of the first member and since the union is only composed of these two structs, I think a cast to the first member should work, but I'm not sure if this is correct or if I'm glossing over padding details in the process. Are unions required to pad the upper bits? It seems as this is unspecified

c union and bitfields

送分小仙女□ 提交于 2019-11-30 08:32:15
问题 Can bitfields be used in union? 回答1: Yes, they can be. Why not? Bit-fields in unions behave in the same way they behave anywhere else. There's nothing special about bit-fields in unions (or unions with bit-fields). 回答2: Yes it is possible, but I would recommend against it. The length and packing of bitfields is not portable. The size of the union will be difficult to predict (see here). There is a certain amount of complexity that you introduce into the code when you use unions or bitfields.

How are the union members stored?

柔情痞子 提交于 2019-11-30 08:31:06
union test { int i; char ch; }t; int main() { t.ch=20; } Suppose sizeof(int)==2 and let the memory addresses allocated for t are 2000, 2001. Then where is 20 i.e. t.ch stored - at 2000 or 2001 or depends on endianness of machine? The C99 standard (§6.7.2.1.14) says: The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa.