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

printf issue

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



Joined: 30 Jan 2012
Posts: 218

View user's profile Send private message

printf issue
PostPosted: Wed May 13, 2015 3:55 am     Reply with quote

Hi everybody

I'm using a PIC18F2550, with PCH V5.015.

I use printf function all the time, and it worked well.

But now my program is big, and i have sometimes issue to print data in a string :
Code:
typedef signed int32 SINT32;

void test(SINT32 latitude){
   char lat[12];
   char lat2[12];

   sprintf(lat,"%2.6w ",latitude);
   sprintf(lat2,"%2.6w ",latitude);

   fprintf(DEBUG_UART,lat);
   fprintf(DEBUG_UART,lat2);
}


when my program is short, it works all the time : I have the same string lat and lat2.
But when my program is big (ROM 89% RAM 14%) sometimes I have not the same string...

I guess it comes from the compiler, but I don't know how to solve this.

I have issue too with "strstr(*int8 s1,*int8 s2)" function in the same case.

Did you already experiment this issue?

What solution can I have for that ?

thanks for your help

Spilz
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Wed May 13, 2015 7:53 am     Reply with quote

Obvious question would be, if you are doing anything else. If (for instance), there is an interrupt running, then a 'memory leak' in it's code, could corrupt a string.

Your format specifiers are wrong, and this can sometimes cause issues with CCS. The number in front of the decimal point on a format specifier, in C, is the _total field width_. You are asking it to put six places after the decimal, in a field that is just two characters wide.....
There is then another problem 'w', only accepts _unsigned_ numbers, not signed.
From the manual:
Code:

w    Unsigned int with decimal place inserted. Specify two numbers for n. The first is a total field width. The second is the desired number of decimal places.
spilz



Joined: 30 Jan 2012
Posts: 218

View user's profile Send private message

PostPosted: Wed May 13, 2015 8:14 am     Reply with quote

Hello Ttelmah,

Thanks for your reply,

Sorry for my bad English, I'm not sure to understand :
What I want to do is :
printf latitude which is a signed integer in 10-6 deg, in deg
40123456 -> "40.123456"
-4123456 -> "-4.123456"

According to what you wrote, it shouldn't works for signed, but it does work..
I don't know why.

For the interrupt, actually there are the same in small and big programs, it doesn't explain why it always works in small program and sometime doesn't work in big program...

As with big program there are issues too with other functions with string like strstr(), I guess there is a problem with compiler.

An other point : with the same program (middle size ^^)
I have no issue with
Code:
char* AT_received;
Main(){
...
AT_received = malloc(250);
...
}

But I have sometimes issue like above when using :

Code:
char AT_received[250];
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Wed May 13, 2015 8:19 am     Reply with quote

5.015, was a fairly 'early' working V5 compiler. So there might be a problem. You should use an at least 'possible' format specifier. However the compiler will be generating the same code for the print functions, so it does suggest a memory leak.
Ttelmah



Joined: 11 Mar 2010
Posts: 19432

View user's profile Send private message

PostPosted: Thu May 14, 2015 3:47 am     Reply with quote

I have just checked, and with both that version, and the current versions, it is the use of the signed int32, that causes the problem. As the manual says, %w does not support this. It partially works, but with some values gives garbage results.

I simply tested for the value being -ve, and if it was output a - sign, and then printed the abs value, and it worked fine (though this does give one space between the sign and the number). So it does go back to only using legal specifiers.
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