<?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 Re: Initializing arrays in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182289#M13404</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Jim:&lt;BR /&gt;&lt;BR /&gt;A really good explanation there on the use of static. I have only one minor quibble, and it has nothing to do with your fine description. It's more of a typo. In your last code example, the line:&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;int a=0; // created on the static. goes away when I leave this function.&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;Should be:&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;int a=0; // created on the *stack*. goes away when I leave this function.&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;As it is, that comment might create some confusion. Hope this helps,&lt;BR /&gt;&lt;BR /&gt;---Tom&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:33:10 GMT</pubDate>
    <dc:creator>J2MEJediMaster</dc:creator>
    <dc:date>2020-10-29T09:33:10Z</dc:date>
    <item>
      <title>Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182278#M13393</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Hi all,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;A program that I am working on uses a number of arrays to hold various&lt;/DIV&gt;&lt;DIV&gt;items such as time, date, etc.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Out of reset, I would like these to be pre-loaded with data which is valid.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;In reading some information, it is suggested that the following code will&lt;/DIV&gt;&lt;DIV&gt;initialize those arrays:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;static int number[3] = { 5, 7, 2 };&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;however, when I put this in and compile it, I don't see any code that&lt;/DIV&gt;&lt;DIV&gt;actually takes the data and puts it into the ram array.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Another method that I have tried is placing the following into a&lt;/DIV&gt;&lt;DIV&gt;function.&amp;nbsp; Bear in mind that I have about 400 bytes (in 56 arrays)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Time1_day1[0]=0x01;&lt;BR /&gt;Time2_day1[1]=0x01;&lt;BR /&gt;Time3_day1[2]=0x07;&lt;BR /&gt;Time4_day1[3]=0x01;&lt;BR /&gt;Time5_day1[4]=0x01;&lt;BR /&gt;Time6_day1[5]=0x01;&lt;BR /&gt;Time7_day1[6]=0x01;&lt;BR /&gt;Time8_day1[7]=0x01;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This works fine up until the 39th line (trying to do it brute force),&lt;/DIV&gt;&lt;DIV&gt;then I get an error message.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Back to the original question:&amp;nbsp; Is there an easy to initialize an&lt;/DIV&gt;&lt;DIV&gt;array with values?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Tim&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 02:53:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182278#M13393</guid>
      <dc:creator>UtopiaTim</dc:creator>
      <dc:date>2008-01-24T02:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182279#M13394</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;The first method should work fine, the reason you might not see it actually being copied into these locations is it is&amp;nbsp;probably being copied&amp;nbsp;in the startup code.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 03:29:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182279#M13394</guid>
      <dc:creator>allawtterb</dc:creator>
      <dc:date>2008-01-24T03:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182280#M13395</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Hi Allawtterb,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I looked into all the code,&amp;nbsp;&amp;amp; didn't see any code (or data tables) that&lt;/DIV&gt;&lt;DIV&gt;would run &amp;amp; put the values into Ram at startup.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Tim&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 03:52:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182280#M13395</guid>
      <dc:creator>UtopiaTim</dc:creator>
      <dc:date>2008-01-24T03:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182281#M13396</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;In my version of CodeWarrior the start up code that would initialize this data is in Start08.c.&amp;nbsp; Have you checked this file?&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 04:17:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182281#M13396</guid>
      <dc:creator>allawtterb</dc:creator>
      <dc:date>2008-01-24T04:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182282#M13397</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;DIV&gt;&lt;/DIV&gt;Thats because you didn't do anything with them.&lt;BR /&gt;The compiler optimized away the initialization since you didn't use the values in the array.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If you did this, then the array would be filled in:&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;static int number[3] = { 5, 7, 2 }; // 100% correct syntax.
  volatile int a;   int i;   for ( i = 0 ;i &amp;lt; 3  ;++i)      a = numbers[i];

// also this works, if you don't feel like counting
int numbers[]={1,2,3};
// if you need to know how many are in it:
int a = sizeof(numbers)/sizeof(int);
// this is calculated at compile time.
// Just wondering, why did you use static? (a quiz)
&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;You want the smallest code possible so don't fault the compiler for saving ram you don't need (because you never used it!)&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="time_text"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by JimDon on &lt;SPAN class="date_text"&gt;2008-01-23&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;03:39 PM&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by JimDon on &lt;SPAN class="date_text"&gt;2008-01-23&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;03:41 PM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:33:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182282#M13397</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2020-10-29T09:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182283#M13398</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Hi Jim,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks.&amp;nbsp; Plugged the code in (removed the -s- from number),&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;but got a C2801 '}' missing.&amp;nbsp; All braces are in the right&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; places.&amp;nbsp;Moved the 'static int xxxxx; to outside the function,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; but kept the rest of the routine inside the funct.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I thought that by merely adding the values to the end of the&lt;/DIV&gt;&lt;DIV&gt;line, it would automatically load the values, as opposed to the&lt;/DIV&gt;&lt;DIV&gt;general case of:&amp;nbsp; static int number[3]; which would just reserve&lt;/DIV&gt;&lt;DIV&gt;the three locations.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Do I need to do&amp;nbsp;one of these&amp;nbsp;little routine for each of the arrays?&amp;nbsp; That's&lt;/DIV&gt;&lt;DIV&gt;a lot of code to run (56 separate arrays).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;What if I just wanted to load&amp;nbsp;all the arrays&amp;nbsp;with the same value (non-zero)?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;- Static -&lt;/DIV&gt;&lt;DIV&gt;Since I placed the array definitions within a function (main.c here), I wanted&lt;/DIV&gt;&lt;DIV&gt;other functions to be able to use them.&amp;nbsp; However, in the final form, I will probably&lt;/DIV&gt;&lt;DIV&gt;put all the arrays with the other variables, and change them to unsigned char.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Tim&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 05:19:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182283#M13398</guid>
      <dc:creator>UtopiaTim</dc:creator>
      <dc:date>2008-01-24T05:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182284#M13399</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;No the routine is just to trick the compiler thinking you are using it.&lt;BR /&gt;Once you add code that really does use it, it will be there.&lt;BR /&gt;&lt;BR /&gt;The other array set up should be outdie the function.&lt;BR /&gt;Onve you have a line of code in a function you can not declare anymore variables:&lt;BR /&gt;&lt;BR /&gt;f()&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;BR /&gt;i = 2;&lt;BR /&gt;int a; // error, can't declare after code...&lt;BR /&gt;}&lt;BR /&gt;If you are still getting an error, show me. It's quit hard to tell from the message form the compiler.&lt;BR /&gt;&lt;BR /&gt;You can't awlays take what the error says at face value - also, many times fixing the first error will fix or change later errors. It has to do with the particlar way "C" grammer is. It is called "context free" and once the parser gets tripped up by a syntax error it is hard for it figure out what you meant.&lt;BR /&gt;So for now just fix the first error. Of course if you notice that you have made othere errors,then fix those too.&lt;BR /&gt;Also, if you click on those icon in the upper right corner, you shut off the warning and get to the errors faster. Some warnings this compiler issues are silly sounding, but quite often it could mean something wrong.&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 05:36:48 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182284#M13399</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2008-01-24T05:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182285#M13400</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Hi Jim,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Hmmm,&amp;nbsp; still having a bit of a problem.&amp;nbsp; Got it to compile, but it&lt;/DIV&gt;&lt;DIV&gt;doesn't init the values.&amp;nbsp; It blows up when enters into the for loop.&lt;/DIV&gt;&lt;DIV&gt;Here's the code:&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;void Initialize_variables(void);&lt;/DIV&gt;&lt;DIV&gt;//*************************&lt;BR /&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp; init variables&lt;BR /&gt;//*************************&lt;/DIV&gt;&lt;DIV&gt;void Initialize_variables(void){&lt;/DIV&gt;&lt;DIV&gt;static int number[3] = {1,2,3};&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;volatile int a;&lt;BR /&gt;&amp;nbsp;int i;&lt;BR /&gt;&amp;nbsp;for (i = 0 ;i &amp;lt; 3 ;++i)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a = number[i];&lt;BR /&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if I use:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;unsigned char number[3] = {1,2,3};&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;(without any code), then it fills below&lt;/DIV&gt;&lt;DIV&gt;the stack.&amp;nbsp; If I put a bunch of the arrays&lt;/DIV&gt;&lt;DIV&gt;into the&amp;nbsp;code, then the arrays will bump&lt;/DIV&gt;&lt;DIV&gt;into the machine registers, giving me a&lt;/DIV&gt;&lt;DIV&gt;read&amp;nbsp;error.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Not sure how&amp;nbsp;to make it put the arrays above&lt;/DIV&gt;&lt;DIV&gt;the stack.&amp;nbsp; Anyhow, think the static is the one&lt;/DIV&gt;&lt;DIV&gt;I want, because I need to manipulate the arrays&lt;/DIV&gt;&lt;DIV&gt;from other functions.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;tim&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 06:59:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182285#M13400</guid>
      <dc:creator>UtopiaTim</dc:creator>
      <dc:date>2008-01-24T06:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182286#M13401</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Tim,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;When you first created your CW project, did you specifically select the option to provide ANSI initialization code?&amp;nbsp; My understanding is that, unless you do this, the startup code created does not initialize the global and static variables.&amp;nbsp;&lt;/FONT&gt; &lt;FONT size="2"&gt;The initialization of each variable will default to zero, unless you provide explicit values within the variable definition.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If you require variables to be accessible from more than one function, they should be global variables.&amp;nbsp; Static variables may, or may not, be accessible, depending on where they are defined.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;You will need to become familiar with the PRM file, used by the linker, in order to control where different variables are placed.&amp;nbsp; Conventionally, the global and static variables should start at a low RAM address, and work upward, and the stack should start at top of RAM (and will work downward).&amp;nbsp; The total RAM available for the device, will usually determine whether the globals&amp;nbsp;should commence within page zero RAM, or this RAM is reserved for special variable that&amp;nbsp;will benefit from the faster access, and more compact code provided by page zero access.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 10:05:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182286#M13401</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2008-01-24T10:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182287#M13402</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hi Mac,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I had sat down to build a small program to do some array testing, and I saw that when&lt;/DIV&gt;&lt;DIV&gt;I created the project.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I was going to send Jim a note to that effect - thanks!!!&amp;nbsp; It's pretty obvious why things are&lt;/DIV&gt;&lt;DIV&gt;doing what they are (stack/array allocation, etc).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks again,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Tim&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 10:47:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182287#M13402</guid>
      <dc:creator>UtopiaTim</dc:creator>
      <dc:date>2008-01-24T10:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182288#M13403</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;IMG alt="" src="file:/C:/DOCUME%7E1/JDONEL%7E1/LOCALS%7E1/Temp/moz-screenshot.jpg" /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;// This code works fine.// Did you call the function and step into it?
// If you never call the function, then the compiler won't bother
// putting any if there.
 void Initialize_variables(void){static int number[3] = {1,2,3};      volatile int a; int i; for (i = 0 ;i &amp;lt; 3 ;++i)    a = number[i];}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;DIV&gt;&lt;I&gt;"unsigned char number[3] = {1,2,3};&lt;/I&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;I&gt;&amp;nbsp;(without any code), then it fills below&lt;/I&gt;&lt;/DIV&gt;&lt;I&gt;the stack. "&lt;/I&gt;&lt;BR /&gt;Which again is 100% correct. Typically, the stack is placed at the top of ram and grows down.&lt;BR /&gt;You can control the stack placement in the prm file.&lt;BR /&gt;"&lt;I&gt;If I put a bunch of the arrays&lt;/I&gt;&lt;DIV&gt;&lt;I&gt;into the&amp;nbsp;code, then the arrays will bump&lt;/I&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;I&gt;into the machine registers, giving me a&lt;/I&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;I&gt;read&amp;nbsp;error&lt;/I&gt;."&lt;BR /&gt;I don't remember what chip you are using. Either you've run out of memory, you used tiny model or we need to adjust your prm file. Post your prm file please.&lt;BR /&gt;&lt;BR /&gt;Now the meaning of static. unfortunately the meaning of the key word static in C and C++ changes depending on how you use it. One thing very different from asm language is the concept of &lt;B&gt;scoping&lt;/B&gt;.&lt;BR /&gt;variables and other items have an attribute referred to as scope, and it means who can "see" the variable.&lt;BR /&gt;&lt;BR /&gt;There are three basic scopes: program, file and function.&lt;BR /&gt;As you know variables defined in a function can only be seen with in that function. This is function scope. Normally variable defined outside of a function have program scope, meaning and file that is linked in could see it as well as all the code in the file where it is declared.&lt;BR /&gt;If you declare a variable outside of a function static, that limits it's scope to only that file.&lt;BR /&gt;This is done to make variable private to that file.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;// The first meaning of static - limiting scope.static int a;  // only code in this file can see 'a'int b;         // any file linked into this program and see b.void (void){ // example of funcction scope.  int c; // only code in this function and see 'c'}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;The second meaning of static is within a function. Normally&amp;nbsp; variables declared in a function are "automatic" this means they are created on the stack and go away when you leave the function.&lt;BR /&gt;When you declare a variable static in a function, it is no longer on the stack and is placed in ram or rom and retains it's value across function calls. However, it's scope is still for that function only.&lt;BR /&gt;You can however return a pointer to it so other code could see it.&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;int*  f(void){   int a=0; // created on the static. goes away when I leave this function.   static int b=0; // created in ram - keeps it's value.   a = 1;    // The next time I  call the function, a will be 0
   b = 1;    // b will still be 1.
  return &amp;amp;b; // ok - lets the caller see b.  return &amp;amp;a; // error - this would be a terrible mistake
             // and will result in a crash or other odd bugs.
(you wouldn't really have two returns, just for illustration)

}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:33:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182288#M13403</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2020-10-29T09:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182289#M13404</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Jim:&lt;BR /&gt;&lt;BR /&gt;A really good explanation there on the use of static. I have only one minor quibble, and it has nothing to do with your fine description. It's more of a typo. In your last code example, the line:&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;int a=0; // created on the static. goes away when I leave this function.&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;Should be:&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;int a=0; // created on the *stack*. goes away when I leave this function.&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;&amp;nbsp;As it is, that comment might create some confusion. Hope this helps,&lt;BR /&gt;&lt;BR /&gt;---Tom&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:33:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182289#M13404</guid>
      <dc:creator>J2MEJediMaster</dc:creator>
      <dc:date>2020-10-29T09:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182290#M13405</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Good post JimDon&lt;DIV&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;BLOCKQUOTE&gt;&lt;DIV&gt;&lt;HR /&gt;JimDon wrote:&lt;BR /&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;The second meaning of static is within a function. Normally&amp;nbsp; variables declared in a function are "automatic" this means they are created on the stack and go away when you leave the function.&lt;BR /&gt;When you declare a variable static in a function, it is no longer on the stack and is placed in ram or rom and retains it's value across function calls. However, it's scope is still for that function only.&lt;BR /&gt;You can however return a pointer to it so other code could see it.&lt;BR /&gt;&lt;HR /&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&lt;DIV&gt;Sometimes this is referred to as Storage Duration and there was a recent article on embedded.com talking about Scope and Storage Duration.&amp;nbsp; The link is below and the article discusses C and C++.&amp;nbsp; It is the second part of a disucssion on Scope, the first part is linked as well.&lt;/DIV&gt;&lt;DIV&gt;Part 1:&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://embedded.com/columns/programmingpointers/202600398?pgno=4" rel="nofollow" target="_blank"&gt;http://embedded.com/columns/programmingpointers/202600398?pgno=4&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;Part 2:&lt;/DIV&gt;&lt;DIV&gt;&lt;A href="http://www.embedded.com/columns/programmingpointers/205203843?pgno=1" rel="nofollow" target="_blank"&gt;http://www.embedded.com/columns/programmingpointers/205203843?pgno=1&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2008 23:59:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182290#M13405</guid>
      <dc:creator>allawtterb</dc:creator>
      <dc:date>2008-01-24T23:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182291#M13406</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Thanks for the corrections an compliments.&lt;BR /&gt;Needless to say I have have a real job too and and do this rather quickly, sometimes to my regret and embarrassment.&lt;BR /&gt;&lt;BR /&gt;Scope implies storage duration, but of course are not exactly the same. In the function f() above 'a' and 'b' have the same scope but different storage durations. I guess you could say that static overrides the storage duration in a function.&lt;BR /&gt;&lt;BR /&gt;Just for the sake of completeness, in c++ static has yet a third meaning.&lt;BR /&gt;- If you declare a member variable static in a class, it means that for all instances of the class there is one and only one copy of the member variable.&lt;BR /&gt;- if you declare a function static, it means that the function is not associated with an instance of the class and can be called with out instantiating an instance. Basically it is just a "C" function that is scoped in a class.&lt;BR /&gt;Quite handy for call backs (like a window hander) or a utility class.&lt;BR /&gt;When I was learning c++ this thru me off, as I thought I already knew what it meant.&lt;BR /&gt;&lt;BR /&gt;If you don't use c++ feel free to ignore.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by JimDon on &lt;SPAN class="date_text"&gt;2008-01-24&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;01:40 PM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2008 02:37:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182291#M13406</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2008-01-25T02:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182292#M13407</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Some further information:&lt;BR /&gt;&lt;BR /&gt;Every variable allocated at file scope (globals) are of static storage duration. It doesn't matter if they have the keyword static in front of them or not. Though if you declare a local variable as static, it will of course get static storage duration as well.&lt;BR /&gt;&lt;BR /&gt;All such variables will live throughout the whole execution of the program. ISO C requires that they are initialized to a value before program startup. If the programmer did this explicitly, as in the original post, they will be set to the values written by the programmer. If the programmer didn't initialize them explicitly, they will -still- be initialized, to zero.&lt;BR /&gt;&lt;BR /&gt;In order for Codewarrior to conform with ISO C, they will need to add the file startup08.c to "copy-down" values to all variables with static storage duration. They also set the stack from there, to avoid complications of ignorant users doing this themselves in the wrong way.&lt;BR /&gt;&lt;BR /&gt;There are however some problems with the copy-down process at startup:&lt;BR /&gt;&lt;BR /&gt;First of all, C was designed for RAM-based Unix systems in office environments. They pretty much assumed that the program was stored in RAM and that there was an OS in the system. Nowadays C has a much wider usage, and there won't necessarily be an OS in the system. This means that some part of the program must run before main() is started (or if you will, before the ISO C-conformant part of the program is started).&lt;BR /&gt;&lt;BR /&gt;One problem is that the copy-down code will add plenty of "startup-lag" at the beginning of the program. Lets say you have several kb of objects with static storage duration: it will take quite some time before they are set, which might be bad for program performance. But worse: during this time, hardware registers will have out-of-reset values, including the COP register. This might in worst case cause unwanted behavior of the MCU.&lt;BR /&gt;&lt;BR /&gt;One such example is all port I/O pins that are not connected to external resistors. I/O pins are always set as inputs out of reset on Freescale micros. If such pins are exposed to ESD during the startup time the MCU might be damaged, as your code has yet to either set them to outputs or to activate internal pull resistors.&lt;BR /&gt;&lt;BR /&gt;The next problem is electronical: you can never rely on RAM cells during long periods of time. The program might in some cases run for years before a certain variable is used. Therefore, you cannot assume that those variables initialized by the copy-down code still have the correct values when using them.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I would recommend getting rid of the "ANSI C startup" for the above reasons.&lt;BR /&gt;&lt;BR /&gt;The solution is to never rely on static initialization, but to instead set the variables in runtime, just before they are used for the first time. This makes the need for static initialization obsolete. Just be careful if you do this, as you won't be able to initialze variables with static storage duration after doing this.&lt;BR /&gt;&lt;BR /&gt;static int x = 5; /* static initilization, unsafe */&lt;BR /&gt;&lt;BR /&gt;static int x;&lt;BR /&gt;x = 5; /* runtime value assignment, safe */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;In safety-critical systems, static initialization is never used, for the above mentioned reasons. An example of such systems are automotive ones, which plenty of the HC(S)08 and HCS12 micros were designed for.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2008 16:34:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182292#M13407</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2008-01-25T16:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing arrays</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182293#M13408</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;"&lt;I&gt;First of all, C was designed for RAM-based Unix systems in office environments.&lt;/I&gt;"&lt;BR /&gt;&lt;BR /&gt;Try core based systems, for real time telephone switching systems.&lt;BR /&gt;RAM was a laboratory idea at the time, and unless you meant central offices, business offices didn't have computers, at least not ones the employees used (other than programmers).&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2008 23:53:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Initializing-arrays/m-p/182293#M13408</guid>
      <dc:creator>JimDon</dc:creator>
      <dc:date>2008-01-25T23:53:12Z</dc:date>
    </item>
  </channel>
</rss>

