Ill start with an example of source code (that i modified for clarity so ignore variables like "someLetter"):
wchar_t *someFunction()
{
wchar_t str[1024] = L"";
for (int i = 0; i < 50; i++)
{
str[i] = someLetter;
}
str[51] = L'\0';
return str;
}
The above code simply adds wchars to w_char array and when for loop is ended, it ends the array with L'\0'. Variable ret should hold the string. However, when I execute the code below, i get nothing (empty string).
wchar_t *result = someFunction();
wcout << result << endl;
This is where it gets weird. If i execute the code mentioned all above, I get nothing. BUT if I add wcout << str << endl;
in someFunction(), everything seems to be working fine i.e. code below does what its supposed to do.
wchar_t *result = someFunction();
wcout << result << endl;
TL:DR
Code below doesnt print out "result". Instead it prints out nothing and result is blank. The problem is fixed if I add wcout << str<< endl;
to someFunction(). Why is that and how can I avoid that.
wchar_t *result = someFunction();
wcout << result << endl;