<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Can&amp;#39;t assign values to array I just made.</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/28534/can-t-assign-values-to-array-i-just-made</link><description> 
I can&amp;#39;t seem to access the arrays I have made: &amp;#39;buff&amp;#39; and &amp;#39;Time&amp;#39;.
I am even unable to initialize them. I am watching the locals and the
variables &amp;#39;i&amp;#39; and &amp;#39;TimeVar&amp;#39; show changes, but nothing seems to affect
the arrays. 

 
Problem: 

 
 // Variables</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/145213?ContentTypeID=1</link><pubDate>Sat, 29 Oct 2011 01:59:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ab843ff6-b951-4a26-9535-1c3cebcd8497</guid><dc:creator>Tamiryan Michael</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I think the arrays gets stored in non-existing RAM, while
smaller data are stored on stack or in registers.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Does the OP run his program on a simulator or the real thing?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/143040?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2011 10:57:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:dcdbb213-87d1-49c9-93bf-42b2ca23e6d1</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
I think the arrays gets stored in non-existing RAM, while smaller
data are stored on stack or in registers.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/131888?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2011 10:52:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0aab1244-5f91-417c-b142-8d153a8ea36a</guid><dc:creator>@Marc Crandall</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello,&lt;/p&gt;

&lt;p&gt;
This sounds strange to me. Are you doing anything &amp;#39;different&amp;#39; with
your memory or start-up?&lt;/p&gt;

&lt;p&gt;
For example where is your stack located?&lt;/p&gt;

&lt;p&gt;
Is the c-library properly initializing the ZI regions?&lt;/p&gt;

&lt;p&gt;
What does you s8 definition look like?&lt;/p&gt;

&lt;p&gt;
Are you using any &amp;#39;special&amp;#39; compiler options? (like --c99?)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/130887?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2011 10:45:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:527ff22b-73af-4bb5-b0aa-9b3609824fba</guid><dc:creator>Kenny D.</dc:creator><description>&lt;p&gt;&lt;p&gt;
@Marc&lt;/p&gt;

&lt;p&gt;
No, that doesn&amp;#39;t work either. What I mean is that when the arrays
are created they have random values. If I try to initialize them
their values don&amp;#39;t change, they are still random values.&lt;/p&gt;

&lt;pre&gt;
s8 buff_1[4];                   // Will have values like { 0x28, 0x32, 0x00, 0x00 }
s8 buff_2[4] = { 0, 0, 0, 0 };  // Doesn&amp;#39;t work, buff_2 will still have random values

buff_1[0] = 0;    // Still shows up as it&amp;#39;s initial value, not 0
buff_1[1] = 1;    // Still shows up as it&amp;#39;s initial value, not 1
buff_1[2] = 2;    // Still shows up as it&amp;#39;s initial value, not 2
buff_1[3] = 3;    // Still shows up as it&amp;#39;s initial value, not 3

if (buff_1[0] == 0)  // Debugger stops on this line. &amp;#39;Step&amp;#39;, &amp;#39;step over&amp;#39;,
{                    // and &amp;#39;step into&amp;#39; don&amp;#39;t leave this line
  ... // Do something
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/127170?ContentTypeID=1</link><pubDate>Fri, 28 Oct 2011 08:07:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ac286690-1a9c-4a29-bc2d-0efcc16dbf89</guid><dc:creator>@Marc Crandall</dc:creator><description>&lt;p&gt;&lt;p&gt;
What do you mean by &amp;quot;Does not work&amp;quot;?&lt;/p&gt;

&lt;p&gt;
Are you indicating the value is not zero in those locations? Or is
that statement not getting executed?&lt;/p&gt;

&lt;p&gt;
Does the following &amp;quot;work&amp;quot; for you?&lt;/p&gt;

&lt;pre&gt;
 s8 buff_1[4];
  s8 buff_2[4] = { 7, 7, 7, 7 };

  buff_1[0] = 7;
  buff_1[1] = 7;
  buff_1[2] = 7;
  buff_1[3] = 7;
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/116563?ContentTypeID=1</link><pubDate>Thu, 27 Oct 2011 10:05:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:189b3c0f-8187-42c7-a449-2edcca8c3f9a</guid><dc:creator>Kenny D.</dc:creator><description>&lt;p&gt;&lt;p&gt;
I am confident those variables haven&amp;#39;t been optimized out because
in the compiler I set the optimization level to 0, &amp;quot;No optimization&amp;quot;.
I also use the variables later in the function to hold a string, then
send the buffer to atoi to convert it into an integer that is then
used to update the real-time clock.&lt;/p&gt;

&lt;p&gt;
If I change my local arrays to static arrays, then the function
will work. If I remove the static keyword, then the function hangs on
the first read from local array.&lt;/p&gt;

&lt;p&gt;
This code is working:&lt;/p&gt;

&lt;pre&gt;
int SetRTC(char *cmd)
{
  static const s8 RTC_MIN[6] = {  0,  0,  0,  1,  1,  9 };
  static const s8 RTC_MAX[6] = { 59, 59, 23, 31, 12, 99 };
  static const s8 MONTH_TO_DAY[12] = { 31, 28, 31, 30, 31, 30,
                                       31, 31, 30, 31, 30, 31 };

  // Variables
  s8  i       = 0;
  s32 TimeVar = 0;
  static s8 buff[4];
  static s8 Time[6];   // { ss, mm, hh, dd, MM, yy }

  // Parse command string
  for(i = 0; i &amp;lt; 6; i++)
  {
    // If buff is NOT a static array:
    //TimeVar = 0;        // Works fine
    //buff[0] = 10;       // Doesn&amp;#39;t change buff[0], but doesn&amp;#39;t cause program to hang
    //TimeVar = buff[1];  // Causes program to hang

    memset(buff, 0, sizeof(buff));  // Clear the buffer

    memcpy(buff, cmd + (i * 2), 2);  // Copy two digits from command to buffer

    Time[i] = atoi(buff);         // Convert buffer to ineger

    if ((Time[i] &amp;lt; RTC_MIN[i]) || // Check min/max values
        (Time[i] &amp;gt; RTC_MAX[i]))
    {
      printf(&amp;quot;\r\nbad RTC date&amp;quot;); // Display error message
      return No_Commands_Run;     // Return without setting RTC
    }
  }

  // Verify the specified month actually has the specified days
  if (MONTH_TO_DAY[Time[4]] &amp;lt; Time[3])
  {
    if ((Time[4] == 2) &amp;amp;&amp;amp; (Time[3] == 29) &amp;amp;&amp;amp; (Time[5] % 4 == 0))
    {
      // February 29th of a leap year, not an error
    }
    else
    {
      printf(&amp;quot;\r\nbad RTC date&amp;quot;);   // Display error message
      return No_Commands_Run;       // Return without setting RTC
    }
  }

  // Calculate the easy ones:
  TimeVar = Time[0]             +  // Seconds
            Time[1]      * 60   +  // Minutes
            Time[2]      * 3600 +  // Hours
           (Time[3] - 1) * 86400;  // Days

  // Add days for earlier months in the year
  for (i = Time[4] - 1; i &amp;gt;= 0; i--)
    TimeVar += MONTH_TO_DAY[i];

  // Add years for previous years since 2000;
  TimeVar += Time[5] * 365 * 86400;

  // Add leap days
  if(Time[4] &amp;lt;= 2)  // If it is Jan or Feb, hold back a year (see next comments)
    TimeVar += ((Time[5] + 3) / 4) * 86500;   // 2000 = +0 leap days
  else
    TimeVar += ((Time[5] + 4) / 4) * 86500;   // 2000 = +1 leap day

  // Set the clock
  RTC_SetCounter(TimeVar); // Set RTC
        RTC_WaitForLastTask();   // Wait for command to complete

  // TODO: Remove after testing
  ReadRTC();

  return Single_Command;
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/105417?ContentTypeID=1</link><pubDate>Thu, 27 Oct 2011 09:30:00 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fa9fbb37-9691-4845-8309-ac9a2ab29fcc</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
How certain are you the compiler didn&amp;#39;t outsmart your code and
simply optimize those variables out of existence?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/79706?ContentTypeID=1</link><pubDate>Thu, 27 Oct 2011 08:50:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c7b643ad-1a11-4354-9675-0795f7864d55</guid><dc:creator>Kenny D.</dc:creator><description>&lt;p&gt;&lt;p&gt;
Sorry, I knew I should have took that &amp;quot;clever&amp;quot; piece out before I
posted because it would distract from the issue. The problem I am
having is not caused by the for-loop. Even when I do something
straight forward I still can&amp;#39;t modify the values. Example:&lt;/p&gt;

&lt;pre&gt;
void func (void)
{
  s8 buff_1[4];
  s8 buff_2[4] = { 0, 0, 0, 0 };  // Does not work

  buff_1[0] = 0;  // Does not work
  buff_1[1] = 0;  // Does not work
  buff_1[2] = 0;  // Does not work
  buff_1[3] = 0;  // Does not work

  ...
}
&lt;/pre&gt;

&lt;p&gt;
I&amp;#39;ve messed around with other ways of creating arrays which work
just fine, but I can&amp;#39;t get locally declared, non-static arrays to
work. On a possibly related note, even structures don&amp;#39;t like being
declared locally:&lt;/p&gt;

&lt;pre&gt;
typedef struct {
  u8 Second;
  u8 Minute;
  u8 Hour;
  u8 Day;
  u8 Month;
  u8 Year;
} DateTime;

void func (void)
{
  s8  i        = 0;
  s32 TimeVar  = 0;
  s8  *buff;
  s8  buff2;
  static s8 buff3 = { 0, 1, 2, 3}; // Works
  s8  Time[6]  = { -1, -1, -1, -1, -1, -1 };  // Does not initialize
  DateTime theTimeIs;

  buff = (s8 *)malloc(4);

  buff[0] = 0;   // Works
  buff2[0] = 0;  // Does not work
  Time[0] = 0;   // Does not work
  theTimeIs.Year = 11; // Does not work
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't assign values to array I just made.</title><link>https://community.arm.com/thread/67023?ContentTypeID=1</link><pubDate>Thu, 27 Oct 2011 02:43:26 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:632ee48c-4f3e-4847-83a2-7b865ad83065</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
What happens when you have a for loop, where your &amp;quot;last&amp;quot; iteration
have value 1 in the loop element.&lt;/p&gt;

&lt;p&gt;
You then assign 1-1 = 0 to that element, and then perform a --
operation on the element.&lt;/p&gt;

&lt;p&gt;
You finally perform new check if element is zero - do you think
you will ever see your loop variable reach zero?&lt;/p&gt;

&lt;p&gt;
memset(buf,0,sizeof(buf)) is a quite good method to zero a buffer.
Why not use it.&lt;/p&gt;

&lt;p&gt;
Or using a &amp;quot;real&amp;quot; loop variable. With a &amp;quot;normal&amp;quot; loop variable,
the compiler can make use of processor registers, producing smaller
code and optionally not have to allocate any memory for the loop
variable. When abusing an element of the array as a loop variable,
the compiler must produce much worse code.&lt;/p&gt;

&lt;p&gt;
Never try &amp;quot;clever&amp;quot; code unless you are very skilled and know
exactly what happens. &amp;quot;clever&amp;quot; code will almost always come back and
haunt you.&lt;/p&gt;

&lt;p&gt;
Working code always wins over &amp;quot;clever&amp;quot; code that is buggy.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>