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

    sprintf broken?

    garethp_1672656

      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?
          jobi

          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

          • 2. Re: sprintf broken?
            garethp_1672656

            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?
              user_1377889

              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

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

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

                • 5. Re: sprintf broken?
                  garethp_1672656

                  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?
                    user_302397898

                    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?
                      garethp_1672656

                      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>"

                      • 8. Re: sprintf broken?
                        user_302397898

                        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.