We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I can't seem to access the arrays I have made: 'buff' and 'Time'. I am even unable to initialize them. I am watching the locals and the variables 'i' and 'TimeVar' show changes, but nothing seems to affect the arrays.
Problem:
// Variables //-- THE FOLLOWING WORKS JUST FINE --// s8 i = 0; s32 TimeVar = 0; //-- THE FOLLOWING DOES NOT INITIALIZE THE ARRAYS --// s8 buff[4] = { 0, 0, 0, 0 }; s8 Time[6] = { -1, -1, -1, -1, -1, -1 }; // { ss, mm, hh, dd, MM, yy } // I know I'm doing something a little weird here, but this is not causing my // my problem, I added this trying to figure out why it is not working. for (buff[0] = 4; buff[0] > 0; buff[0]--) // Clear the buffer { //-- HANGS HERE --// buff[buff[0] - 1] = 0; }
Complete function:
/******************************************************************************* * Function Name : SetRTC * Description : Sets the realtime clock baised on the 12 byte command input * Input : "ssmmhhddmmyy" * "ss" - Seconds ["00" to "59"] * "mm" - Minutes ["00" to "59"] * "hh" - Hours ["00" to "23"] * "dd" - Day ["01" to "31"] * "MM" - Month ["01" to "12"] * "yy" - Year ["09" to "99"] * Output : Displays an error message if it parses unexpected values * Return : 0 - Error, RTC not set * 1 - Success, RTC set * * Notes : The time is represented as the number of seconds after * 00:00:00 Jan 1, 2000 * * Example : "M361014251011" * Time - 14:10:36 (2:10:36 pm) * Day - 25 October 2011 * RTC Value - 372,867,036 == 1639 7FDC ******************************************************************************/ int SetRTC(char *cmd) { // TODO: Should these be #define? const short RTC_MIN[6] = { 0, 0, 0, 1, 1, 9 }; const short RTC_MAX[6] = { 59, 59, 23, 31, 12, 99 }; const short MONTH_TO_DAY[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Variables //-- THE FOLLOWING WORKS JUST FINE --// s8 i = 0; s32 TimeVar = 0; //-- THE FOLLOWING DOES NOT INITIALIZE THE ARRAYS --// s8 buff[4] = { 0, 0, 0, 0 }; s8 Time[6] = { -1, -1, -1, -1, -1, -1 }; // { ss, mm, hh, dd, MM, yy } // I know I'm doing something a little weird here, but this is not causing my // my problem, I added this trying to figure out why it is not working. for (buff[0] = 4; buff[0] > 0; buff[0]--) // Clear the buffer { //-- HANGS HERE --// buff[buff[0] - 1] = 0; } // Parse command string for(i = 0; i < 6; i++) { memcpy((&buff[0]), cmd + (i * 2), 2); // Copy two digits from command to buffer Time[i] = atoi(&buff[0]); // Convert buffer to ineger if ((Time[i] < RTC_MIN[i]) || // Check min/max values (Time[i] > RTC_MAX[i])) { printf("\r\nbad RTC date"); // 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]] >= Time[3]) { if ((Time[4] == 2) && (Time[3] == 29) && (Time[5] % 4 == 0)) { // February 29th of a leap year, not an error } else { printf("\r\nbad RTC date"); // 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] - 2; i >= 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] <= 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_Configuration(); // Initialize RTC RTC_WaitForLastTask(); // Wait for command to complete RTC_SetCounter(TimeVar); // Set RTC RTC_WaitForLastTask(); // Wait for command to complete // TODO: Remove after testing ReadRTC(); return Single_Command; }
What happens when you have a for loop, where your "last" iteration have value 1 in the loop element.
You then assign 1-1 = 0 to that element, and then perform a -- operation on the element.
You finally perform new check if element is zero - do you think you will ever see your loop variable reach zero?
memset(buf,0,sizeof(buf)) is a quite good method to zero a buffer. Why not use it.
Or using a "real" loop variable. With a "normal" 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.
Never try "clever" code unless you are very skilled and know exactly what happens. "clever" code will almost always come back and haunt you.
Working code always wins over "clever" code that is buggy.
Sorry, I knew I should have took that "clever" 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't modify the values. Example:
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 ... }
I've messed around with other ways of creating arrays which work just fine, but I can't get locally declared, non-static arrays to work. On a possibly related note, even structures don't like being declared locally:
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 }
How certain are you the compiler didn't outsmart your code and simply optimize those variables out of existence?
I am confident those variables haven't been optimized out because in the compiler I set the optimization level to 0, "No optimization". 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.
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.
This code is working:
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 < 6; i++) { // If buff is NOT a static array: //TimeVar = 0; // Works fine //buff[0] = 10; // Doesn't change buff[0], but doesn'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] < RTC_MIN[i]) || // Check min/max values (Time[i] > RTC_MAX[i])) { printf("\r\nbad RTC date"); // 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]] < Time[3]) { if ((Time[4] == 2) && (Time[3] == 29) && (Time[5] % 4 == 0)) { // February 29th of a leap year, not an error } else { printf("\r\nbad RTC date"); // 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 >= 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] <= 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; }
What do you mean by "Does not work"?
Are you indicating the value is not zero in those locations? Or is that statement not getting executed?
Does the following "work" for you?
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;
@Marc
No, that doesn'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't change, they are still random values.
s8 buff_1[4]; // Will have values like { 0x28, 0x32, 0x00, 0x00 } s8 buff_2[4] = { 0, 0, 0, 0 }; // Doesn't work, buff_2 will still have random values buff_1[0] = 0; // Still shows up as it's initial value, not 0 buff_1[1] = 1; // Still shows up as it's initial value, not 1 buff_1[2] = 2; // Still shows up as it's initial value, not 2 buff_1[3] = 3; // Still shows up as it's initial value, not 3 if (buff_1[0] == 0) // Debugger stops on this line. 'Step', 'step over', { // and 'step into' don't leave this line ... // Do something }
Hello,
This sounds strange to me. Are you doing anything 'different' with your memory or start-up?
For example where is your stack located?
Is the c-library properly initializing the ZI regions?
What does you s8 definition look like?
Are you using any 'special' compiler options? (like --c99?)
I think the arrays gets stored in non-existing RAM, while smaller data are stored on stack or in registers.
Does the OP run his program on a simulator or the real thing?