This example invokes undefined behavior.
operator<<(std::wostream&,const wchar_t*)
expects the buffer to be completed with a zero value and stop printing characters when it reaches the first character L'\0'
. If the buffer contains a null character ( L'\0'
), then the code will work "correctly" (although the output is unpredictable). If this is not the case, operator<<
will continue to read the memory until it encounters it.
Having a null terminator does not apply in your example. For comparison, the following will print an unspecified number of characters, most likely garbage, but clearly defined:
WCHAR wArray[1024]; wArray[1023] = L'\0'; wcout << wArray << endl;
source share