8 Replies Latest reply on Jun 30, 2016 9:07 AM by JeCo_264681

    sprintf broken?

      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.

        • 1. Re: sprintf broken?



          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.




          JOBIN GT

          • 2. Re: sprintf broken?

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

            • 3. Re: sprintf broken?

              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.





              1 of 1 people found this helpful
              • 4. Re: sprintf broken?

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

                • 5. Re: sprintf broken?

                  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.

                  • 6. Re: sprintf broken?

                    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.

                    • 7. Re: sprintf broken?

                      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:




                      But the second block will give:


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

                      • 8. Re: sprintf broken?

                        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.