I noticed a weird problem while using wcout in a console application.
After calling a certain function, the rest of the wcout calls did not work at all. i.e. the output statements did not appear on the console.
I noticed that in the function, I had used a wide character array which was never assigned.
WCHAR wArray[1024];
wcout<<wArray<<endl;
It was after this call, all the other wcout’s stopped working.
So, I was just curious to know what makes wcout different from cout, and why this problem occured,
This example invokes undefined behavior.
operator<<(std::wostream&,const wchar_t*)expects the buffer to be null terminated, and stop printing characters when it reaches the firstL'\0'character. If the buffer happens to contain a null character (L'\0'), then the code will run “correctly” (although the output is unpredictable). If it doesn’t, thenoperator<<will keep reading the memory until it encounters one.The presence of a null terminator is not enforced by your example. In comparison, the following would print an unspecified number of characters, most likely junk, but is well defined: