0

This code from my project. I am not able to understand from where buffer is getting this (潓敭慮敭(10):) garbage value after using with swprintf_s.

    const char* m_filename = "Somename";
    unsigned m_line = 10;
    wchar_t buffer[256];

    ZeroMemory(buffer, 256);

    auto count = swprintf_s(buffer, L"%S(%d): ",
        m_filename, m_line);

I am using Visual Studio 2015 Preview.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137

1 Answers1

1

swprintf_s expects wchar_t string as '%S' argument and you are passing char string, thus treating two chars as single wchar_t (i.e.: 'So' -> '潓').

Desu_Never_Lies
  • 690
  • 3
  • 10
  • With `wprintf`, %s means a wide string and %S means a narrow string. See [https://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx](https://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx). Is this behaviour different between `wprintf` and `swprintf_s`? – user253751 Feb 20 '15 at 06:13
  • It shouldn't. I wouldn't trust MS docs so much though, see this: http://stackoverflow.com/questions/17700797/printf-wprintf-s-s-ls-char-and-wchar-errors-not-announced-by-a-compil – Desu_Never_Lies Feb 20 '15 at 06:19