sprintf broken?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

The following works as expected:

   

iBufUsed += sprintf(tx_buff + iBufUsed, "%u,ACK,", *(volatile uint8 *) &iMyUnitID);
    iBufUsed += sprintf(tx_buff + iBufUsed, "%s,", sSequenceID);

   

 

   

But this is broken:

   

iBufUsed += sprintf(tx_buff + iBufUsed, "%u,ACK,%s,", *(volatile uint8 *) &iMyUnitID, sSequenceID);

   

 

   

In the broken example the final string has garbage where the string pointed to by sSequenceID should be. It's going to be hard to remember which things don't work as they should.

0 Likes
1 Solution
JobinT_31
Employee
Employee
50 solutions authored 25 solutions authored 10 solutions authored

Hello,

   

Is it possible to share more info in this forum. This can happen if the output string length of sprintf exceed the destination string length. Can you check this.

   

Thanks

   

JOBIN GT

View solution in original post

0 Likes
8 Replies
JobinT_31
Employee
Employee
50 solutions authored 25 solutions authored 10 solutions authored

Hello,

   

Is it possible to share more info in this forum. This can happen if the output string length of sprintf exceed the destination string length. Can you check this.

   

Thanks

   

JOBIN GT

0 Likes
Anonymous
Not applicable

We were about 20 characters into a 1024 byte buffer. Clearly the buffer is amply sized because doing it in two function calls works.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

More or less a guess: Increase the heap size to 0x0200 bytes.

   

When nothing helps, consider to post your complete project, so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

Anonymous
Not applicable

I can tell you from personal experience heap size is the problem. Increase as needed until sprintf behaves.

0 Likes
Anonymous
Not applicable

Other than that bundle appearing to contain all our IP and not just the project settings that sounds like a great idea.

   

I will attempt the heap size fix when I next touch this.

0 Likes

It is probably the case that heap size is the problem as others have said. You can increase this in the System tab of the cydwr window. Having said that, I'm not sure what you mean by the variable containing garbage. If you are printing the variables out in a terminal window, be sure your UART parameters agree with your term parameters. Also be sure your code starts the UART and its interrupt: UART_1_Start(); and isr_1_Start(); for example. I once forgot to put in these lines and I got garbage on the screen.

0 Likes
Anonymous
Not applicable

By garbage I mean something other than the contents of my string. if myUID is 8 and sSequence is "ABCD" the first block will give a string of:

   

"8,ACK,ABCD"

   

But the second block will give:

   

"8,ACK,<a few unprintable characters, variable each time it's run>"

0 Likes

Assuming you are using UART and a terminal program to read the output, be sure to connect P0[1] to TX, both by physical wire on the development board (assuming you are using one) and in your cydwr.
If you were using the GNU compiler I would say to check build settings and go to Linker > General and make newlib-nano and newlib-nano Float Format both True. From your other posts, I think you might be using the Keil compiler. I'm not sure what the equivalent settings there are.

0 Likes