unions

Difference between a Structure and a Union

老子叫甜甜 提交于 2019-11-26 00:29:36
问题 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 there any other OS level difference? 回答1: With a union, you're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location

Purpose of Unions in C and C++

南楼画角 提交于 2019-11-26 00:13:51
问题 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; uint8_t r; uint8_t a; } components; } pixel; pixel.colour = 0xff040201; // ARGB::colour is the active member from now on // somewhere down the line, without any edit to pixel if(pixel.components.a) // accessing the non-active member ARGB::components is actually undefined behaviour I.e. reading from a member

Accessing inactive union member and undefined behavior?

安稳与你 提交于 2019-11-25 23:56:50
问题 I was under the impression that accessing an union member other than the last one set is UB, but I can\'t seem to find a solid reference (other than answers claiming it\'s UB but without any support from the standard). So, is it undefined behavior? 回答1: The confusion is that C explicitly permits type-punning through a union, whereas C++ (c++11) has no such permission. c11 6.5.2.3 Structure and union members 95) If the member used to read the contents of a union object is not the same as the

Is type-punning through a union unspecified in C99, and has it become specified in C11?

故事扮演 提交于 2019-11-25 22:35:43
问题 A number of answers for the Stack Overflow question Getting the IEEE Single-precision bits for a float suggest using a union structure for type punning (e.g.: turning the bits of a float into a uint32_t ): union { float f; uint32_t u; } un; un.f = your_float; uint32_t target = un.u; However, the value of the uint32_t member of the union appears to be unspecified according to the C99 standard (at least draft n1124), where section 6.2.6.1.7 states: When a value is stored in a member of an

Why does C++ disallow anonymous structs?

我怕爱的太早我们不能终老 提交于 2019-11-25 19:14:25
Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful. What's the rationale that prevents this from being part of the standard? Is there a technical roadblock? A philosophical one? Or just not enough of a need to justify it? Here's a sample of what I'm talking about: struct vector3 { union { struct { float x; float y; float z; }; float v[3]; }; }; My compiler will accept this, but it warns that "nameless struct/union" is a non-standard extension to C++ . As others have pointed out anonymous unions