4

_vscprintf is not available on Android. Also vsprintf(NULL, fmt, ap) does not work (produces seg fault), so there seems to be no way of calculating size of buffer required for vsnprintf to succeed?

Android sources indicate that __android_log_print function just truncates strings to 1024 using vsnprintf...

How do you handle this scenario?

miha
  • 3,287
  • 3
  • 29
  • 44
  • 3
    "vsprintf(NULL, fmt, ap) does not work (produces seg fault)": are you sure you're not using it wrong? – Mat Oct 19 '11 at 17:48

1 Answers1

13

Section [7.19.6.13]—The vsprintf function—of the C99 Standard does not state that the output buffer may be NULL.

You probably want to use vsnprintf:

int len = vsnprintf(NULL, 0, fmt, ap)

If the call is successful, the return value is the number of characters that would have been written if the buffer were large enough, excluding the NUL terminator. This is like _vscprintf, which also does not include the NUL terminator in its count.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • 1
    I got that suggestion from SO post(http://stackoverflow.com/questions/4785381/replacement-for-ms-vscprintf-on-macos-linux), so I tried it even though I've read the docs... – miha Oct 19 '11 at 21:19
  • And thanks for bringing that up, I overlooked that part of the documentation of vsnprintf, which I am actually using to produce the resulting string. – miha Oct 19 '11 at 21:49