|
|
View previous topic :: View next topic |
Author |
Message |
Maverick Guest
|
Bug with variables allocation?? |
Posted: Tue Feb 10, 2004 8:13 am |
|
|
I had a problem recently and found a solution to resolve it.
But I still have question on my mind.
The problem was like this:
I was formating a float into a string with sprintf and was sending each char to an LCD with a for loop .
sprintf( sBuffer, "\f%2.2fAE" , fCurrent );
for ( i=0 ;sBuffer[i]!='E' ;i++)
{
lcd_putc(sBuffer[i]);
}
It works first and was showing 0.00A to the lcd, (it is a current monitor).
But until recently I added some variables to the project and it stops to work! It's like the for loop wasnt able to see the 'E' at the end of the formatted string to end the loop and it was showing craps on my lcd.
The order of my variables in the file were like this:
int
long
float
char sBuffer[8]
To resolve the problem I changed the order of the variable, so now its
char sBuffer[8]
float
long
int
Can someone tell me if its a bug of CCS or something else??
I use version 3.129 of CCS. Thx |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Feb 10, 2004 10:14 am |
|
|
Not sure on your problem, but since you are dealing with a string it has a NULL terminator. so the 'E' is not needed. You can just simply check for 0. |
|
|
Ttelmah Guest
|
|
Posted: Tue Feb 10, 2004 1:14 pm |
|
|
Mark wrote: | Not sure on your problem, but since you are dealing with a string it has a NULL terminator. so the 'E' is not needed. You can just simply check for 0. |
And, importantly, the storage area allocated is too small...
The string has a form feed (one character), the five numeric digits (with the decimal point), the letter 'A', and then the letter 'E', _followed by the null terminator_. Total _nine_ characters. Sprintf, adds the null terminator to the output string, so the last character would be dropping out the end of the allocated area, and overwritng whatever is there...
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Feb 10, 2004 2:14 pm |
|
|
Missed the form feed. Any value 10.0 and over will cause problems. Also note that the size of the array should be allocated depending on the largest value for fCurrent. 9 would work okay up to 99.99 but fail on 100.00. Of course, I doubt your current is that high. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|