{"id":898,"date":"2022-08-30T15:09:01","date_gmt":"2022-08-30T15:09:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/union-reading-from-one-data-member-of-a-union-to-write-into-another-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:01","modified_gmt":"2022-08-30T15:09:01","slug":"union-reading-from-one-data-member-of-a-union-to-write-into-another-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/union-reading-from-one-data-member-of-a-union-to-write-into-another-collection-of-common-programming-errors\/","title":{"rendered":"Union: Reading from one data member of a union to write into another-Collection of common programming errors"},"content":{"rendered":"<p>I know that for the code below, &#8220;Illegal&#8221; below is undefined (while some compilers allow it), because union member &#8220;a&#8221; is active, and then we read from union member &#8220;b&#8221;. The question is, does the code in &#8220;AmILegal&#8221; fix it, or am I doing something scary and even more obscure? Can I use memcpy to achieve the same effect or is there another undefined behaviour I am invoking there?<\/p>\n<p>EDIT: Maybe the example is not clear enough. All I want to do is activate the other member. So I am changing float to int. Although it seems dumb, it is closer to the real case. Read BELOW the code.<\/p>\n<p>(Is it for some reason disallowed to copy one union member into another?)<\/p>\n<pre><code>struct Foo\n{\n    union Bar\n    {\n        int a[4];\n        int b[4];\n    };\n\n    void this_is_Illegal()\n    {\n         a[0]=1;\n         a[1]=2;\n         a[2]=3;\n         a[3]=4;\n         std::cout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:55:58. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I know that for the code below, &#8220;Illegal&#8221; below is undefined (while some compilers allow it), because union member &#8220;a&#8221; is active, and then we read from union member &#8220;b&#8221;. The question is, does the code in &#8220;AmILegal&#8221; fix it, or am I doing something scary and even more obscure? Can I use memcpy to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}