<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Compiler: Bug of feature (struct/union initialization)</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/96535/compiler-bug-of-feature-struct-union-initialization</link><description>Hello, 
 I observed a strange behaviour which I would rate as a Compiler bug but it could be also a language feature which I didn&amp;#39;t know until now. 
 I have the following defines: 
 
 When I create now an instance of the union and initialize it, the first</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Feb 2023 08:15:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/96535/compiler-bug-of-feature-struct-union-initialization" /><item><title>RE: Compiler: Bug of feature (struct/union initialization)</title><link>https://devzone.nordicsemi.com/thread/409285?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2023 08:15:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:490f0862-8d6e-4e72-8a6d-97f6a3a562e6</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;Way worse: Using different members of a union is &lt;strong&gt;undefined&lt;/strong&gt; behavior in C.&lt;/p&gt;
&lt;p&gt;With the GCC compiler its actually defined behavior in some cases when using the uint8_t byte types - at least it was when I red the docs last time.&lt;/p&gt;
&lt;p&gt;You can also try using memory barriers between the union value setting and the print() statements.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler: Bug of feature (struct/union initialization)</title><link>https://devzone.nordicsemi.com/thread/409284?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2023 08:13:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24dbeee9-3c9f-4c29-a58e-0f0c63ea732f</guid><dc:creator>Einar Thorsrud</dc:creator><description>[quote user="ErwinCes"]Ok. With different words: when initializing a union I&amp;#39;m just allowed to use one item of the union. It&amp;#39;s not allowed to use two different items in the same initialization list. Correct?[/quote]
&lt;p&gt;Yes, that is right. A struct, has all the members. A union is different, and can only hold one of the members. This can be very useful sometimes, particularily when making more generic APIs, but you should not confuse it with a struct.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler: Bug of feature (struct/union initialization)</title><link>https://devzone.nordicsemi.com/thread/409276?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2023 07:53:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:275f74ed-3ec8-4dfc-90d9-92b7c9039ba2</guid><dc:creator>ErwinCes</dc:creator><description>&lt;p&gt;Ok. With different words: when initializing a union I&amp;#39;m just allowed to use one item of the union. It&amp;#39;s not allowed to use two different items in the same initialization list. Correct?&lt;/p&gt;
&lt;p&gt;I searched before in the internet and haven&amp;#39;t found this explanation/limitation. Or I simply didn&amp;#39;t understand the text correctly.&lt;/p&gt;
&lt;p&gt;But this limitation seems to be specified inside c and therefore it&amp;#39;s not a compiler bug. It might also not be so strictly specified so that some other compilers might support this mixed initialization. I will remember this limitation now for the future.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Compiler: Bug of feature (struct/union initialization)</title><link>https://devzone.nordicsemi.com/thread/409272?ContentTypeID=1</link><pubDate>Fri, 10 Feb 2023 07:38:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2c0e38f8-493b-4d1a-a1e4-f2e476ec446b</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;In C unions there is a single memory&amp;nbsp;location for the union, which is scaled to the largest member (so while it looks like a struct it actually is something very different).&amp;nbsp; And you should only use a single member of a single instance of the union. In the &amp;quot;failing&amp;quot; case here you are using two members of the same instance. The first two elements of the u8 does not overlap with the second element of the u16,&amp;nbsp;so you could think that this should work, and&amp;nbsp;probably it will with some compilers with different optimization configuration. But I do not think you can call it a compiler bug when the compiler does not behave as you would want when your code itself does not&amp;nbsp;comply with the language.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>