0

I am calling a function which outputs a wide character in the output stream. Consider:-

wcout<<GetOutput();

and the definition of function is like this:-

const wchar_t* GetOutput()
{
        std::wstringstream stringStream;
        stringStream << out_name << " " << out_type << out_subtype;
        wchar_t* check = new wchar_t[stringStream.str().length() + 1];
        wcscpy(check, stringStream.str().c_str());
        return check;
}

Now, when this functions is called on cmd, it is giving me correct output. But, when the same function is called on linux(WSL), I am getting output as an address which is something like this:-

0x1ef110ad0

Why is this happening?

Edit:- The definition of variables are as follows:-

const std::wstring out_name = L"My Name";
const std::wstring out_type = L"Type1";
const std::wstring out_subtype = L"SubType1";
  • What is `out_name`? What is `out_type`? What is `out_subtype`? Why do you use the narrow-character string `" "` for the space? – Some programmer dude Jul 08 '21 at 07:18
  • @Rohit Nagpal what does wprintf gives you? –  Jul 08 '21 at 07:23
  • 1
    What is `wout`? Do you mean `std::wcout`? You also have memory leaks – Mircea Ispas Jul 08 '21 at 07:26
  • @Someprogrammerdude out_name and other variables are just wstring which are assigned value. – Rohit Nagpal Jul 08 '21 at 07:29
  • @vish wprintf doesn't output anything in linux but in windows it works. – Rohit Nagpal Jul 08 '21 at 07:30
  • @MirceaIspas Sorry, it was a typo. Edited it. wcout and std::wcout both didn't worked. – Rohit Nagpal Jul 08 '21 at 07:33
  • What happens if you change the narrow-character string `" "` to the wide-character string `L" "`? Or the wide character `L' '`? – Some programmer dude Jul 08 '21 at 07:38
  • @Someprogrammerdude Now, I am getting garbage value. – Rohit Nagpal Jul 08 '21 at 07:45
  • 1
    What is the contents of your variables? And please try to create a [mcve] to show us. Also, why do you return a pointer from `GetOutput`? Why not a `std::wstring` object? – Some programmer dude Jul 08 '21 at 07:47
  • @RohitNagpal could you check what charset you are using? If MBCS, I think it **may** make sense that it would print address. –  Jul 08 '21 at 08:01
  • @vish After removing MBCS and inserting _UNICODE, address is not getting printed, but the thing is output now contains garbage value. – Rohit Nagpal Jul 08 '21 at 08:45
  • @RohitNagpal good to know. Do note, you need to define both `_UNICODE` & `UNICODE`. Give it a try and let us know. https://stackoverflow.com/questions/7953025/why-both-unicode-and-unicode#:~:text=_UNICODE%3A,of%20%22...%22%20. –  Jul 08 '21 at 09:27
  • @vish I am using both in my character set, but still having issues. – Rohit Nagpal Jul 08 '21 at 09:28
  • @RohitNagpal okay, could you tell us what are types and content of `out_name , out_type, out_subtype`? –  Jul 08 '21 at 09:31
  • @vish I have edited the question. Please check. – Rohit Nagpal Jul 08 '21 at 09:37
  • @RohitNagpal If I understand correctly, this would be printing on remote machine on linux terminal right? In any case, if this is a big application, I would ask you to make a tiny one with `main`, `GetOutput`, with minimum headers, test it. If you get same results post it along with information about whether you are using DEBUG or RELEASE, Processor configuration, x86, x64, ARM, ARM64. I would think your best bet though is change one thing a time. Also make sure to run that app without VS remote debugging on that machine. –  Jul 08 '21 at 10:06
  • @vish Thanks for the advice buddy. I will try that. – Rohit Nagpal Jul 08 '21 at 10:18

0 Answers0