1

Today, I faced some weird issue with ARM toolchain EABI (cross-compiler).

Ths issue is when i try to use snwprintf() for converting floating point value to string , i was getting some junk string which does not have actual floating point value that i passed.

smaple code looke like this:

float floatValue = 1.0;
snwprintf (buffer, bufferSize, _T("%g"), floatValue);

I then debugged snwprintf and i found out that variable argument list (va_arg) is not pointing to exact data value. And by dumping the memory we were able to find the exact data present in the variable argument list. The data that should be pointed by va_arg is differing.

I don't think this is an endianess issue as integre value is working fine. The problem is only with double values.

Can anyone help me in this issue?

John3136
  • 28,809
  • 4
  • 51
  • 69
Santhosh
  • 637
  • 1
  • 7
  • 19
  • 1
    dont you need %lg for doubles? otherwise it may put twice as much stuff on the stack or in passed in parameters and the printf only takes half of that parameter. do you have -Wall on your gcc command line? – old_timer Oct 12 '12 at 05:38

1 Answers1

1

You should format/print it as a double, since floats are widened to doubles for variadic functions.

Procedure Call Standard for the ARM Architecture states following

5.5 Parameter Passing
A variadic function is always marshaled as for the base standard.

7.2 Argument Passing Conventions
For variadic functions, float arguments that match the ellipsis (…) are converted to type double.

Afaik 7.2 holds for C in general.

auselen
  • 27,577
  • 7
  • 73
  • 114
  • @dwelch was faster than me however I didn't want to waste my research on various ARM documents :) – auselen Oct 12 '12 at 06:11
  • If this is indeed mismatched formatting and variable types as I suspect it has nothing to do with ARM it is a generic C issue that you see on most or all platforms (once you cross the boundary from a single parameter entry size, for example 32 bit registers or stack locations holding a 64 bit item, printf thinks there is a single 32 bit item, compiler has consumed 2 32 bit items because of the variable type). – old_timer Oct 12 '12 at 14:23
  • dwelch - Can you suggest some solution? – Santhosh Oct 15 '12 at 04:14