unions

Union vs. static_cast(void*)

99封情书 提交于 2019-11-29 16:18:16
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 not the way c++ code should be designed. As I read, void pointers shouldn't be used unless there's no

What are the benefits of unnamed structs / unions in C?

百般思念 提交于 2019-11-29 13:36:56
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 . But What are benefits of it ? Because anyway I can access the data members in a same manner like int main() { struct st s; s.a=11; s.b=22; return 0; } compiled on gcc 4.5.2 with , gcc -Wall demo.c -o demo and no errors , It does not have to be an anonymous struct inside a struct, which I do not find very useful: this will typically only change the layout slightly by introducing more padding, with no other

How are the union members stored?

拟墨画扇 提交于 2019-11-29 12:33:52
问题 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? 回答1: 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

Is a union in C++ actually a class?

試著忘記壹切 提交于 2019-11-29 11:48:17
问题 A junior developer asked me if it was possible to overload assignment operators for a union with POD arguments such that the corresponding data type within the union would get written to when an instance of the union is assigned to variable of that type. I replied that I did not think so, but then played around with the following code. To my surprise this code actually compiled (using g++ version 4.6.3 on Ubuntu 12.04) union unMember { float fData; unsigned int uiData; unMember():uiData(0) {}

C++: unions with methods?

北战南征 提交于 2019-11-29 11:47:25
问题 Is there anything wrong with a union having one or more methods? Or anything to watch out for? (I can see constructors/destructors being problematic for schizophrenic reasons) 回答1: From the C++03 & C++0x (Draft N3092) standards: 9.5 Unions A union can have member functions (including constructors and destructors) , but not virtual (10.3) functions . A union shall not have base classes. A union shall not be used as a base class. Initializing the union using the aggregate initializer syntax ( U

Memory layout of union of different sized member?

拟墨画扇 提交于 2019-11-29 11:08:23
问题 typedef union epoll_data { void *ptr; int fd; __uint32_t u32; __uint64_t u64; } epoll_data_t; Here int and __uint32_t are 4 bytes,while the others are 8 bytes. When we set fd to an int ,does it lie on the first 4 bytes or the last 4 bytes,or does it depend on endianness? Some reason is appreciated. 回答1: It lies on the first 4 bytes. From the C99 standard §6.7.2.1/14: 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

How to write destructor for union-like class

心已入冬 提交于 2019-11-29 10:24:40
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 destructor, and as I'm using this union on the stack, the compiler errors that the destructor is deleted. The union is as follows: struct LuaVariant { LuaVariant() : type(VARIANT_NONE) { } LuaVariantType_t type; union { std::string text; Position pos; uint32_t number; }; }; The type variable holds what field of the union is being used (chosen from an enum

Are there any guarantees for unions that contain a wrapped type and the type itself?

谁都会走 提交于 2019-11-29 09:32:47
Can I put a T and a wrapped T in an union and inspect them as I like? union Example { T value; struct Wrapped { T wrapped; } wrapper; }; // for simplicity T = int Example ex; ex.value = 12; cout << ex.wrapper.wrapped; // ? The C++11 standards only guarantee save inspection of the common initial sequence, but value isn't a struct . I guess the answer is no , since wrapped types aren't even guaranteed to be memory compatible to their unwrapped counterpart and accessing inactive members is only well-defined on common initial sequences . I believe this is undefined behavior. [class.mem] gives us:

How to use c union nested in struct with no name

久未见 提交于 2019-11-29 08:09:26
问题 I'm working on the so called Hotspot open source project, and looking at the implementation I found a nasty nested union in struct looking like that: typedef struct RC_model_t_st { union { struct block_model_t_st *block; struct grid_model_t_st *grid; }; /* block model or grid model */ int type; thermal_config_t *config; }RC_model_t; As far as I'm aware in C/C++ that union is unaccesible. So how someone can make use of union declared in such manner and for what purpose? Thanks! 回答1: This is an

Union of structs with common first member

自作多情 提交于 2019-11-29 07:32:18
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 behavior? Does anyone have any insight as to whether this is suported. I know that there is an