<?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: Overwrited variable</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173263#M5502</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can just declare your own "heap" space with a static variable, like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;static int heap[25];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That will give you space, integer aligned, that is 100 bytes in size (25*4), and the compiler won't put anything else there.&amp;nbsp; You can then keep track of who is using what portions of that space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternately, you can allocate the space in the linker command file, but that's probably more work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 19 Apr 2009 20:04:43 GMT</pubDate>
    <dc:creator>RichTestardi</dc:creator>
    <dc:date>2009-04-19T20:04:43Z</dc:date>
    <item>
      <title>Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173256#M5495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;in main function, I declare a pointer called MY_VAR. It points to type called TYPE, it is a structure. In function called from main function there is set a variable of type TYPE and the function returns the address of this variable. But if my program continues, another call of some function destroys the data stored on address where MY_VAR pointer points. How to tell the compiler not to destroy the data in *MY_VAR?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
void main(void) {&amp;nbsp; TYPE *MY_VAR;&amp;nbsp; MY_VAR = setFunction();&amp;nbsp; anotherFunction(); // it destroys data in *MY_VAR}TYPE *setFunction() {&amp;nbsp; TYPE result;&amp;nbsp; // result = ......&amp;nbsp; return &amp;amp;result;}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:21:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173256#M5495</guid>
      <dc:creator>gaminn</dc:creator>
      <dc:date>2020-10-29T09:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173257#M5496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unless I completely misunderstand you, setFunction() is returning a pointer to a variable on its stack, which is implicitly deallocated (and available for reuse)&amp;nbsp;by the time setFunction() returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd suggest that you either make "result" static in setFunction(), or allocate it on the stack&amp;nbsp;in main() and pass its address to setFunction(), rather than vice versa.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does that make sense?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- Rich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 18 Apr 2009 05:40:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173257#M5496</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2009-04-18T05:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173258#M5497</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's a good explanation of the problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Wild_pointer" rel="nofollow" target="_blank"&gt;http://en.wikipedia.org/wiki/Wild_pointer&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Search for "returning addresses of a stack-allocated local variable".&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 18 Apr 2009 05:50:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173258#M5497</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2009-04-18T05:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173259#M5498</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm begginner, so sorry for that stupid question. Static keyword is what helps me, partially...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to deallocate this static variables. My program gets to the stage where the static variable is not needed. I guess it is not possible, so is there any way to dynamically allocate variables?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 18 Apr 2009 14:03:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173259#M5498</guid>
      <dc:creator>gaminn</dc:creator>
      <dc:date>2009-04-18T14:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173260#M5499</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can't deallocate a static variables...&amp;nbsp; Local variables are dynamically allocated on the stack and are implicitly deallocated when the function returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You may be able to use malloc() and free() to allocate heap variables dynamically and free them when you no longer need them, depending on which libraries you are linking to and the heap settings in your linker command file (lcf).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See if there is a document like "MSL C Reference" or maybe a Compiler manual in your Help/PDF directory from CodeWarrior -- I'm not sure what version of CW you are using, or for what MCU.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- Rich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 08:13:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173260#M5499</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2009-04-19T08:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173261#M5500</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As Rich already suggested, you can pass the address of a local into setFunction instead of retuning it.&lt;/P&gt;&lt;P&gt;Depending on what TYPE is, the "setFunction" (quotes as it is more a get-function actually) could also return TYPE, and not TYPE*.&lt;/P&gt;&lt;P&gt;I would not go with static for various reasons, one is, as you noted, statics cannot be deallocated. Another reason is that there is only a single static instance ever, whereas with locals different function invocations have their own copy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using free/malloc is technically possible, but I would not do it on a small chip. Leaking memory can cause problematic bugs and it also makes the code harder to get correct as the caller now must remember to call free. Also dynamically allocated memory has to managed, that causes additional overhead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
void main(void) {
  TYPE MY_VAR;

  setFunction(&amp;amp;MY_VAR);

  anotherFunction();
}

void setFunction(TYPE* result) {
  // ......
  *result = ......

  return;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:21:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173261#M5500</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2020-10-29T09:21:47Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173262#M5501</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;OK, you said that there are problems with using standard malloc and free functions. So I would like to program my own malloc and free functions, little memory manager... Only&amp;nbsp;I need is to tell compiler not to store data to memory area (tens of bytes)&amp;nbsp;where I intend to store my dynamic variables. How to do it?&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 14:46:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173262#M5501</guid>
      <dc:creator>gaminn</dc:creator>
      <dc:date>2009-04-19T14:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173263#M5502</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can just declare your own "heap" space with a static variable, like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;static int heap[25];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That will give you space, integer aligned, that is 100 bytes in size (25*4), and the compiler won't put anything else there.&amp;nbsp; You can then keep track of who is using what portions of that space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternately, you can allocate the space in the linker command file, but that's probably more work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 20:04:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173263#M5502</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2009-04-19T20:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrited variable</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173264#M5503</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Yes, that was stupid question, I realized that it is simple as you said a minute after I switched my computer off. &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; So thank you everybody, I think I know what I wanted to know.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 20:11:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Overwrited-variable/m-p/173264#M5503</guid>
      <dc:creator>gaminn</dc:creator>
      <dc:date>2009-04-19T20:11:41Z</dc:date>
    </item>
  </channel>
</rss>

