Search code examples
c++visual-studiogarbagewidestring

Why buffer is getting garbage value?


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.


Solution

  • 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' -> '潓').