<?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>LPC Microcontrollers中的主题 Re: Compiler optimization for size (-Os) eats my code. Why?</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204341#M43373</link>
    <description>&lt;P&gt;Pardon the delay,&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/26034"&gt;@xiangjun_rong&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;Here's the shortest code that fails. Put a watch on raw_switches_index and monitor its value on each successive systick. It should count 0,1,2,0,1,2,0,1,2... but I have had variations of 0,1,2,1,2,1,2,1,2 and 0,1,2,3,0,1,2,3,0,1,2,3... depending on compiler optimization settings and included libraries/semihosting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The .txt file contains the disassembled code.&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
    <pubDate>Sat, 26 Dec 2020 23:24:07 GMT</pubDate>
    <dc:creator>tomchr</dc:creator>
    <dc:date>2020-12-26T23:24:07Z</dc:date>
    <item>
      <title>Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1200682#M43286</link>
      <description>&lt;P&gt;I'm using the latest MCUXpresso IDE and am writing a bit of code to read a control bus. Nothing fancy. I debounce the bus for a bit of extra EMI/RFI robustness. The code is basically straight from&amp;nbsp;&lt;SPAN&gt;Ganssle&lt;/SPAN&gt;, J.G. (2004) &lt;EM&gt;A Guide to &lt;/EM&gt;&lt;SPAN&gt;&lt;EM&gt;Debouncing&lt;/EM&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;The code works fine with the 'debug' configuration, but when I switch to 'release', the code breaks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've traced it down to compiler optimization. Optimization levels of -O0 to -O3 work. The optimization for size (-Os) breaks my code. I'm trying to understand why.&lt;/P&gt;&lt;P&gt;Here's my code:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;#define&lt;/STRONG&gt;&lt;/SPAN&gt; DEBOUNCE_CHECKS 10u&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;* Global variables&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;******************************************************************************/&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint32_t&lt;/SPAN&gt; systick_counter;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; raw_switches[DEBOUNCE_CHECKS], raw_controlBus[DEBOUNCE_CHECKS];&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; raw_switches_index = 0;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;void&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;STRONG&gt;SysTick_Handler&lt;/STRONG&gt;(&lt;SPAN&gt;&lt;STRONG&gt;void&lt;/STRONG&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt; (systick_counter != 0U)&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;{&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;systick_counter--;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;// Read control bus&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;raw_controlBus[raw_switches_index] = 0;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt;(GPIO_PortRead(GPIO, BOARD_INITPINS_REMOTE_PORT) &amp;amp; BOARD_INITPINS_REMOTE_PIN_MASK) {&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;raw_controlBus[raw_switches_index] |= CTRL_REMOTE_BIT;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt;(GPIO_PortRead(GPIO, BOARD_INITPINS_CTRL5_PORT) &amp;amp; BOARD_INITPINS_CTRL5_PIN_MASK) {&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;raw_controlBus[raw_switches_index] |= CTRL_CAP_BIT2_BIT;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;[......]&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt;(raw_switches_index++ &amp;gt; DEBOUNCE_CHECKS) {&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;raw_switches_index = 0;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;The issue is the in the last if() statement. raw_switches_index starts at 0 (as initialized) and counts 1, 2, 3 ... 10, &lt;EM&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/EM&gt;, 2, 3 ... 10, &lt;EM&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/EM&gt;, 2, 3 ... It was supposed to count 0, 1, 2 ,3 ... 10, &lt;STRONG&gt;&lt;EM&gt;0&lt;/EM&gt;&lt;/STRONG&gt;, 1, 2, 3 ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why is this happening and how can I avoid it? I can certainly run without compiler optimization or with -O3, but I would like to &lt;EM&gt;understand&lt;/EM&gt; what's going on here. I've tried taking the increment outside the if(), so:&lt;/P&gt;&lt;P&gt;raw_switches_index++;&lt;/P&gt;&lt;P&gt;if(raw_switches_index &amp;gt; DEBOUNCE_CHECKS) ....&lt;/P&gt;&lt;P&gt;I've tried pre-incrementing and I've tried raw_switches_index = raw_switches_index + 1. The compiler just laughs at me, which is not very nice. &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway. I'm really curious what could cause this. The reduction in code size that results for the compiler optimization is nice, but the reduction in functionality is not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Wed, 16 Dec 2020 20:37:11 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1200682#M43286</guid>
      <dc:creator>tomchr</dc:creator>
      <dc:date>2020-12-16T20:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1200696#M43287</link>
      <description>&lt;P&gt;Update:&amp;nbsp;&lt;/P&gt;&lt;P&gt;The 'release' setup with compiler optimization of -Os results in 6952 B of code but loses functionality as described above.&lt;/P&gt;&lt;P&gt;Changing the optimization to -O3 and the library from redlib(semihost-nf) to redlib(none) results in 6544 B of code and this code has the desired functionality.&lt;/P&gt;&lt;P&gt;Go figure....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Wed, 16 Dec 2020 20:53:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1200696#M43287</guid>
      <dc:creator>tomchr</dc:creator>
      <dc:date>2020-12-16T20:53:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201117#M43295</link>
      <description>&lt;P&gt;Hi, Tom,&lt;/P&gt;
&lt;P&gt;The release version of the project only selects "Optimization for size(-Os)" for the Optimization Level as the following figure. for size optimization, if you has function body, but you do not call the function, the function will be removed from the *.elf. For the variable for example hardware status which may be modified by hardware, you'd better add volatile before the variable.&lt;/P&gt;
&lt;P&gt;for example volatile uint32_t capture_Value;&lt;/P&gt;
&lt;P&gt;Hope it can help you&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="xiangjun_rong_0-1608195782639.png" style="width: 400px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/132953i9D30B600C6FD4979/image-size/medium?v=v2&amp;amp;px=400" role="button" title="xiangjun_rong_0-1608195782639.png" alt="xiangjun_rong_0-1608195782639.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 09:10:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201117#M43295</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2020-12-17T09:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201212#M43297</link>
      <description>&lt;P&gt;Thank you for your response.&lt;/P&gt;&lt;P&gt;In my case the function is the systick interrupt service routine. All the variables it works on are declared as volatile.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 11:56:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201212#M43297</guid>
      <dc:creator>tomchr</dc:creator>
      <dc:date>2020-12-17T11:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201310#M43298</link>
      <description>&lt;P&gt;It is possible, although unlikely, that there is a compiler bug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you disassemble the working and non-working object files and post them? (right-click on the .o file and select Binary Utilities-&amp;gt;Disassemble)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, if you could provide a minimum working example that demonstrates the problem.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 15:22:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201310#M43298</guid>
      <dc:creator>converse</dc:creator>
      <dc:date>2020-12-17T15:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201462#M43302</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;
&lt;P&gt;for the line &lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; raw_switches_index = 0;, can you change it as:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; raw_switches_index;&lt;/P&gt;
&lt;P&gt;void main()&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;.........................&lt;/P&gt;
&lt;P&gt;raw_switches_index=0;&lt;/P&gt;
&lt;P&gt;....................&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;when you declare a variable as raw_switches_index=0, it must be saved in flash and copy to RAM as a variable.&lt;/P&gt;
&lt;P&gt;Pls have a try.&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 01:10:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1201462#M43302</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2020-12-18T01:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204341#M43373</link>
      <description>&lt;P&gt;Pardon the delay,&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/26034"&gt;@xiangjun_rong&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;Here's the shortest code that fails. Put a watch on raw_switches_index and monitor its value on each successive systick. It should count 0,1,2,0,1,2,0,1,2... but I have had variations of 0,1,2,1,2,1,2,1,2 and 0,1,2,3,0,1,2,3,0,1,2,3... depending on compiler optimization settings and included libraries/semihosting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The .txt file contains the disassembled code.&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Sat, 26 Dec 2020 23:24:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204341#M43373</guid>
      <dc:creator>tomchr</dc:creator>
      <dc:date>2020-12-26T23:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204371#M43374</link>
      <description>&lt;P&gt;With your example, I cannot reproduce the problem (at least, I get the same behaviour regardless of optimisation level). However, I think I see the problem in your code:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt; (raw_switches_index++ &amp;gt; 2) {&lt;BR /&gt;    raw_switches_index = 0;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;this code is post-increment, so it checks raw_switches_index is &amp;gt;2 and THEN increments it, so if the value is 2 beforehand, it can (quite validly) now have a value of 3.&lt;/P&gt;&lt;P&gt;If you want to restrict its value to 0, 1 or 2, then you should pre-increment&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt; (++raw_switches_index &amp;gt; 2) {&lt;BR /&gt;    raw_switches_index = 0;&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Dec 2020 18:06:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204371#M43374</guid>
      <dc:creator>converse</dc:creator>
      <dc:date>2020-12-27T18:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: Compiler optimization for size (-Os) eats my code. Why?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204375#M43375</link>
      <description>&lt;P&gt;I'm now not able to reproduce the 'broken' behaviour either. For a while I did have raw_switches_index count differently depending on optimization level. But now its counting behaviour is the same (broken) behaviour regardless op optimization level.&lt;/P&gt;&lt;P&gt;Following your comment about pre- vs post-increment, I think I've found the problem. Here's a code snippet:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;#define&lt;/STRONG&gt;&lt;/SPAN&gt; MAX_INDEX 2u&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; array[MAX_INDEX];&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;volatile&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;uint8_t&lt;/SPAN&gt; raw_switches_index;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;void&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;STRONG&gt;SysTick_Handler&lt;/STRONG&gt;(&lt;SPAN&gt;&lt;STRONG&gt;void&lt;/STRONG&gt;&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;array[raw_switches_index] = 0;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;if&lt;/STRONG&gt;&lt;/SPAN&gt;(raw_switches_index++ &amp;gt; MAX_INDEX) {&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;raw_switches_index = 0;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;This is a problem as I will end up writing past the end of the array when raw_switches_index reaches 3. That overwrites the value of raw_switches_index with the value 0. Now, why this would allow the code to work with no compiler optimization (in fact it worked all the way to -O3) only to break when the code was optimized for size (-Os) is beyond me. It would be nice if I could reproduce it in a smaller code snippet.&lt;/P&gt;&lt;P&gt;Either way, I'll leave it for now and go fix my code. Note to self: Don't write to random locations in memory. Unpredictable behaviour may result.&amp;nbsp;I can tell it's been a good decade since I last did any programming. &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help. Happy New Year.&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Sun, 27 Dec 2020 21:51:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Compiler-optimization-for-size-Os-eats-my-code-Why/m-p/1204375#M43375</guid>
      <dc:creator>tomchr</dc:creator>
      <dc:date>2020-12-27T21:51:07Z</dc:date>
    </item>
  </channel>
</rss>

