<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>CodeWarrior for MCUのトピックRe: Warning C5651</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168103#M5041</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The reason you get the warning is that you pass by value; for some reason CW treats bitfields different than common structs when they are passed by value.&lt;BR /&gt;&lt;BR /&gt;It is common programming practice to always pass struct types by reference in C, no matter their size. If you pass it by reference, the warning disappears for some reason.&lt;BR /&gt;&lt;BR /&gt;That being said, the char type is not allowed for bitfields by ANSI/ISO C, it is listed as "undefined behavior", meaning it will behave completely randomly. Apart from the standard, further issues with bitfields are that they are completely unspecified regarding bit alignment, byte alignment, how the default "int" is treated, padding bytes, etc etc. Bitfields are therefore completely unreliable even when they follow standard C.&lt;BR /&gt;&lt;BR /&gt;The only serious solution is to use bitwise operators. They will yield -exactly- the same machine code, but they will behave in a deterministic way and will be portable to any C/C++ compiler in the world.&lt;BR /&gt;&lt;BR /&gt;Something like this perhaps:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;typedef unsigned char typeparam;

enum /* bit masks */
{
  par1   = 0xF0,
  par2   = 0x0F,
};

enum /* bit values */
{
  par1_1 = 0x10,
  par1_2 = 0x20,
  par2_1 = 0x01,
  par2_2 = 0x02,
};

...

void main()
{
 typeparam data;
 char ret;

 data = par1_2 | par2_1;

 ret = fun(data);
 ...
}

char fun(typeparam param)
{
 if((param &amp;amp; par1) == par1_1)
 {
   return 1;
 }
 ...
}
&lt;/PRE&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by Lundin on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2009-03-30&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;04:57 PM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:15:44 GMT</pubDate>
    <dc:creator>Lundin</dc:creator>
    <dc:date>2020-10-29T09:15:44Z</dc:date>
    <item>
      <title>Warning C5651</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168102#M5040</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using CodeWarrior v.5.9.0 (build 2404) with an MC9S08QE64 MCU and in my project I get the warning:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A target="_blank"&gt;&lt;/A&gt;C5651: Local variable &amp;lt;variable&amp;gt; may be not initialized&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the piece of code written to stimulate the warning:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-style-span"&gt;typedef&amp;nbsp;struct{&lt;BR /&gt;&amp;nbsp;char&amp;nbsp;par1&amp;nbsp;:4;&lt;BR /&gt;&amp;nbsp;char&amp;nbsp;par2&amp;nbsp;:4;&lt;BR /&gt;}typeparam;&lt;BR /&gt;&lt;BR /&gt;char&amp;nbsp;fun(typeparam&amp;nbsp;param);&lt;BR /&gt;&lt;BR /&gt;void&amp;nbsp;main(void)&amp;nbsp;{&lt;BR /&gt;&amp;nbsp;typeparam&amp;nbsp;data;&lt;BR /&gt;&amp;nbsp;char&amp;nbsp;ret;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;EnableInterrupts;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;data.par1&amp;nbsp;=&amp;nbsp;2;&lt;BR /&gt;&amp;nbsp;data.par2&amp;nbsp;=&amp;nbsp;1;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;ret&amp;nbsp;=&amp;nbsp;fun(data);&lt;BR /&gt;&amp;nbsp;for(;; )&amp;nbsp;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;__RESET_WATCHDOG();&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;char&amp;nbsp;fun(typeparam&amp;nbsp;param){&lt;BR /&gt;&amp;nbsp;if(param.par1&amp;nbsp;==&amp;nbsp;1)&amp;nbsp;return&amp;nbsp;1;&lt;BR /&gt;&amp;nbsp;if(param.par2&amp;nbsp;==&amp;nbsp;2)&amp;nbsp;return&amp;nbsp;2;&lt;BR /&gt;&amp;nbsp;return&amp;nbsp;3;&lt;BR /&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable data in main function &amp;nbsp;seems to be initialized but I get the warning:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;A target="_blank"&gt;&lt;/A&gt;C5651: Local variable "data" may be not initialized&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could someone explain me why?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Mirko.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Mar 2009 20:36:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168102#M5040</guid>
      <dc:creator>mimais</dc:creator>
      <dc:date>2009-03-30T20:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Warning C5651</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168103#M5041</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The reason you get the warning is that you pass by value; for some reason CW treats bitfields different than common structs when they are passed by value.&lt;BR /&gt;&lt;BR /&gt;It is common programming practice to always pass struct types by reference in C, no matter their size. If you pass it by reference, the warning disappears for some reason.&lt;BR /&gt;&lt;BR /&gt;That being said, the char type is not allowed for bitfields by ANSI/ISO C, it is listed as "undefined behavior", meaning it will behave completely randomly. Apart from the standard, further issues with bitfields are that they are completely unspecified regarding bit alignment, byte alignment, how the default "int" is treated, padding bytes, etc etc. Bitfields are therefore completely unreliable even when they follow standard C.&lt;BR /&gt;&lt;BR /&gt;The only serious solution is to use bitwise operators. They will yield -exactly- the same machine code, but they will behave in a deterministic way and will be portable to any C/C++ compiler in the world.&lt;BR /&gt;&lt;BR /&gt;Something like this perhaps:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;typedef unsigned char typeparam;

enum /* bit masks */
{
  par1   = 0xF0,
  par2   = 0x0F,
};

enum /* bit values */
{
  par1_1 = 0x10,
  par1_2 = 0x20,
  par2_1 = 0x01,
  par2_2 = 0x02,
};

...

void main()
{
 typeparam data;
 char ret;

 data = par1_2 | par2_1;

 ret = fun(data);
 ...
}

char fun(typeparam param)
{
 if((param &amp;amp; par1) == par1_1)
 {
   return 1;
 }
 ...
}
&lt;/PRE&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by Lundin on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2009-03-30&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;04:57 PM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:15:44 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168103#M5041</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2020-10-29T09:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Warning C5651</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168104#M5042</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much.&lt;/P&gt;&lt;P&gt;Mirko.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Apr 2009 19:33:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Warning-C5651/m-p/168104#M5042</guid>
      <dc:creator>mimais</dc:creator>
      <dc:date>2009-04-02T19:33:35Z</dc:date>
    </item>
  </channel>
</rss>

