Purpose of Unions in C and C++

I have used unions earlier comfortably; today I was alarmed when I read [this post][1] 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 of the union other than the one recently written to leads to undefined behaviour. If this isn't the intended usage of unions, what is? Can some one please explain it elaborately? **Update:** I wanted to clarify a few things in hindsight. - The answer to the question isn't the same for C and C++; my ignorant younger self tagged it as both C and C++. - After scouring through C++11's standard I couldn't conclusively say that it calls out accessing/inspecting a non-active union member is undefined/unspecified/implementation-defined. All I could find was §9.5/1: > If a standard-layout union contains several standard-layout structs that share a common initial sequence, and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members. §9.2/19: Two standard-layout structs share a common initial sequence if corresponding members have layout-compatible types and either neither member is a bit-field or both are bit-fields with the same width for a sequence of one or more initial members. - While in C, ([C99 TC3 - DR 283][2] onwards) it's legal to do so ([thanks to Pascal Cuoq][3] for bringing this up). However, attempting to do *it can still lead to undefined behavior*, if the value read happens to be invalid (so called "trap representation") for the type it is read through. Otherwise, the value read is implementation defined. - C89/90 called this out under unspecified behavior (Annex J) and K&R's book says it's implementation defined. Quote from K&R: > This is the purpose of a union - a single variable that can legitimately hold any of one of several types. [...] so long as the usage is consistent: the type retrieved must be the type most recently stored. It is the programmer's responsibility to keep track of which type is currently stored in a union; the results are implementation-dependent if something is stored as one type and extracted as another. - Extract from Stroustrup's TC++PL (emphasis mine) > Use of unions can be essential for compatness of data [...] *sometimes misused for "type conversion*". *Above all, this question (whose title remains unchanged since my ask) was posed with an intention of understanding the purpose of unions AND not on what the standard allows* E.g. Using inheritance for code reuse is, of course, allowed by the C++ standard, but [it wasn't the purpose or the original intention of introducing inheritance as a C++ language feature][4]. This is the reason Andrey's answer continues to remain as the accepted one. [1]: https://stackoverflow.com/questions/1812348/a-question-about-union-in-c [2]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_283.htm [3]: https://stackoverflow.com/questions/2310483/purpose-of-unions-in-c-and-c#comment26826326_2313676 [4]: https://isocpp.org/wiki/faq/objective-c#objective-c-and-inherit

以上就是Purpose of Unions in C and C++的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏