CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Bug with variables allocation??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Maverick
Guest







Bug with variables allocation??
PostPosted: Tue Feb 10, 2004 8:13 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 10, 2004 10:14 am     Reply with quote

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







PostPosted: Tue Feb 10, 2004 1:14 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 10, 2004 2:14 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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