This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS570LS3137: Problem using vsnprintf for float values

Part Number: TMS570LS3137


Tool/software: TI C/C++ Compiler

Hello Everyone,

I am using HDK(TMS570LS3137). I want to use vsnprintf to write the float values to the result_buffer. Here is the code

#define MAX_BUFFER_SZ (3*1024)

char result_buffer[MAX_BUFFER_SZ];
char * current_ptr=result_buffer;
bool buffer_overflow=false;

int main()
{
    printf("Sample %f", 1067.786876f);

    return 0;
} 

extern "C" int printf(const char * frmt, ...)
{
   signed int remaining_bytes= (result_buffer + sizeof(result_buffer))-current_ptr;
   signed int num_chars_added = 0;

   if (remaining_bytes > 0)
   {
       va_list vl;
       va_start(vl, frmt);

       // Print info the output buffer for as many bytes are are left
       num_chars_added = vsnprintf(current_ptr, remaining_bytes, frmt, vl);
       va_end(vl);

       // Did we add characters to the output buffer, if yes then
       // advance the current_ptr to the next valid location where the next data
       // will be added
       if ( num_chars_added > 0)
       {
           current_ptr = current_ptr + num_chars_added;
       }

       // vsnprintf returns actual number of bytes that might have been
       // copied if the destination buffer was large enough to store the
       // string, but the size of the buffer gives was less, a buffer overflow
       // condition, so it this happens then set the flag
       if( num_chars_added > remaining_bytes)
       {
           buffer_overflow = true;
       }
   }
   return num_chars_added;
}

I see the float values are not written correctly into the result_buffer.

result_buffer contains : Sample 0.0000000

I am linking library rtsv7R4_T_be_v3D16_eabi.lib. Is there any thing need to be done ?

Thanks