Original Post
I''m developing a game for windows CE (pocket PC) that is coming along great, but I am experiencing some bizarre behavior that I can''t quite figure out how to get around. First of all, winCE uses wide char (WCHAR or wchar_t), which is unicode, instead of ascii, so every string has to be converted to unicode using a TEXT() macro, like this:
I have a function DrawText(WCHAR* string) that draws the string to the screen every frame.
Here is the problem:
Now, for some reason, "something different" will be drawn onscreen after the first frame. How can this be? I''m passing a constant to DrawText, so how could it change? It seems to me that because the constant string passed to DrawText is the same as the one pointed to by otherString, the compiler is assigning them to the same area in memory, so when I change otherString I am effectively changing the constant TEXT("nothing special"). Is this behavior normal? I certainly didn''t expect it to behave that way.
So, what do I do now? This is a problem, because whenever I initialize or reset a string like this:
WCHAR* text = TEXT("");
or
text = TEXT("");
...I am effectively making all WCHAR*''s point to the same memory location. When I change one, all the rest that have at some point been set to TEXT("") will change as well!
Not only that, but I seem to have a whole lot of memory leaks resulting from this, because strings will be filled with nonsense sometimes after I change a string. What do I do? I tried storing all my strings as WCHAR[], but I run into problems passing these to other functions and changing them.
I realize that strcpy and strcat (which are actually wcscpy and wcscat under the unicode set of funcs for winCE) exist for some reason, but I have always just changed strings by the methods shown above. I tried initializing and changing my strings with wcscpy, but that doesn''t seem to work either.
I''m sure I''ll figure it out eventually, but if anyone could help find a solution, I''d be very gratefull.
WCHAR string1[] = TEXT("This will work. string1 is now an array of wide chars.");
WCHAR* string2 = TEXT("This will also work. This is just like char* and is a pointer to text");
string2 = TEXT("This will change what string2 points to");
string1 = TEXT("This will not work and may cause a mem leak. Maybe it would work if the size of the array was the same");
DrawText(TEXT("nothing special"));
WCHAR* otherString = TEXT("nothing special");
otherString = TEXT("something different");