<?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>topic Why can't I allocate an array of function pointers into FLASH? in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132548#M3082</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Hi, using CodWarrior 3.1 for HC(S)08, I am trying to get an array of function pointers allocated into FLASH (.rodata).&amp;nbsp; Instead they are allocated into RAM (.data) and initialization code loads the array.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Here is what I am doing:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;const void (*sapfnIPHandlers [])(void) =&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func1,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func2,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func3,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;...&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;funcn&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;void func1 (void)&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;...&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;etc.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have tried the -Cc compiler option with no change in the output.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This code is simply a way of parsing some commands to get to the right function.&amp;nbsp; What am I doing wrong (or what is the compiler doing wrong)?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 15 Apr 2006 02:09:13 GMT</pubDate>
    <dc:creator>wre</dc:creator>
    <dc:date>2006-04-15T02:09:13Z</dc:date>
    <item>
      <title>Why can't I allocate an array of function pointers into FLASH?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132548#M3082</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Hi, using CodWarrior 3.1 for HC(S)08, I am trying to get an array of function pointers allocated into FLASH (.rodata).&amp;nbsp; Instead they are allocated into RAM (.data) and initialization code loads the array.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Here is what I am doing:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;const void (*sapfnIPHandlers [])(void) =&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func1,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func2,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;func3,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;...&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;funcn&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;void func1 (void)&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;...&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;etc.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have tried the -Cc compiler option with no change in the output.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This code is simply a way of parsing some commands to get to the right function.&amp;nbsp; What am I doing wrong (or what is the compiler doing wrong)?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2006 02:09:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132548#M3082</guid>
      <dc:creator>wre</dc:creator>
      <dc:date>2006-04-15T02:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Why can't I allocate an array of function pointers into FLASH?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132549#M3083</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Function pointers are normally stored in RAM, here is a workaround:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;const unsigned int pFunction[]={(unsigned int)&amp;amp;SafePoint,(unsigned int)&amp;amp;BlackButton};&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;and you call it this way:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;((void (*)(void))pFunction[0])();&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Cheers&lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2006 03:09:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132549#M3083</guid>
      <dc:creator>tequila</dc:creator>
      <dc:date>2006-04-15T03:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: Why can't I allocate an array of function pointers into FLASH?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132550#M3084</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The reason why the array ends up in RAM is a common error in C programming.&lt;BR /&gt;For pointers (including function pointers), qualifiers have to be after the *, hence this array:&lt;BR /&gt;&amp;gt;const void (*sapfnIPHandlers [2])(void);&lt;BR /&gt;if not constant. You can assign to it, try&lt;BR /&gt;&lt;BR /&gt;&amp;gt;void test(void) { sapfnIPHandlers[0]= fun; }&lt;BR /&gt;&lt;BR /&gt;The correct version is:&lt;BR /&gt;&amp;gt;void (*const sapfnIPHandlers [2])(void)= {fun0, fun1};&lt;BR /&gt;&lt;BR /&gt;Well this works, but it is a bit hard to read in my point of view.&lt;BR /&gt;So for functions, I would use a typedef instead:&lt;BR /&gt;&lt;BR /&gt;&amp;gt;typedef void (*FunctionType)(void);&lt;BR /&gt;&amp;gt;const FunctionType sapfnIPHandlers[2]= {fun0, fun1};&lt;BR /&gt;&lt;BR /&gt;Bye&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2006 07:40:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132550#M3084</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-04-15T07:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: Why can't I allocate an array of function pointers into FLASH?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132551#M3085</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;Many thanks for both responses.&amp;nbsp; The first one got me going without using RAM and the second simplifies things...although I will have to study it a bit more to see what it is actually doing...&lt;/P&gt;&lt;P&gt;typedef void (*FunctionType)(void);&lt;/P&gt;&lt;P&gt;OK, I see that it matches an example from K&amp;amp;R such that it is creating the type FunctionType which is a pointer to a function with no arguments and returns nothing.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Message Edited by wre on &lt;SPAN class="date_text"&gt;04-18-2006&lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;11:54 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Apr 2006 22:42:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Why-can-t-I-allocate-an-array-of-function-pointers-into-FLASH/m-p/132551#M3085</guid>
      <dc:creator>wre</dc:creator>
      <dc:date>2006-04-18T22:42:40Z</dc:date>
    </item>
  </channel>
</rss>

