-1

I am encountering an error when I try to use memcpy on a wchar_t string. Specifically, despite the length I am sending in to memcpy being correct for the length of the string I want to copy, only the first half of the characters in the string are copied over. This leaves me with bad data in the target string, since the latter half of the string is full of garbage characters left over from new-ing up the string.

Screenshot below shows the exact problem I am dealing with:

screenshot of code with watches showing the values I get when my code runs The only thing I can think of is that this might be somehow related to me having my locale set to Japanese for non-Unicode applications, but the first thing I did was to change the locale back to English and the problem remains the same. I feel like there is a really obvious solution that I am missing. Any ideas?

CodeRedd
  • 283
  • 1
  • 4
  • 14

2 Answers2

6
  • memcpy takes a number of bytes, not a number of objects
  • instead of memcpy using wcslen(src) + 1, there is wcscpy
  • Instead of new wchar_t[] there is std::wstring.

If you had made m_pTextures be a vector of std::wstring then your code would be a whole lot simpler and less prone to errors like this.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thank you for the guidance! Honestly, I am very new to C++ so knowing how to make my code simpler and less error-prone is very helpful! – CodeRedd Jun 11 '14 at 23:55
3

As the others have already pointed out, memcpy copies bytes, and wchar_t is more than one byte. Regardless of that, why don't you use some of the existing functions for this? Have a look at wcscpy and the whole lot of functions intended for wchar_t. Start here.

imbtfab
  • 493
  • 4
  • 10