pointer to char problem

Started by
3 comments, last by Rian 20 years, 11 months ago
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:
  
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");
  
I have a function DrawText(WCHAR* string) that draws the string to the screen every frame. Here is the problem:
  
DrawText(TEXT("nothing special"));
WCHAR* otherString = TEXT("nothing special");
otherString = TEXT("something different");
  
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.
I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it. -Jack Handy
Advertisement
you need to allocate memory for each character of your string
some ways
WCHAR str[]={TEXT("HAM")};  -------------------------WCHAR str[3];wstrcpy(str,TEXT("HAM"));-------------------------WCHAR *str = new WCHAR[3];wstrcpy(str,TEXT("HAM"));delete [] str;-------------------------   





[edited by - quack on April 30, 2003 1:19:24 PM]

[edited by - quack on April 30, 2003 1:20:28 PM]
www.autodefrost.com/~travisSignature Virus cleaned by McAfee
WCHAR *str = new WCHAR[3];

hmm... I haven''t tried that, but I guess that probably is the easiest solution. But then the problem arises of how to figure out the size the array should be. Doesn''t it have to be exactly equal in size to the string? I mean, 3 is obvious for your case, because you know the size of HAM. But what if you dont know what the string is until run-time?

Wait a minute. If I just allocate enough memory for each WCHAR*, enough so I will never run out of room (say 100 chars), can I still set it to, say TEXT("HAM") as long as I re-allocate memory with ''new'' before I change it? I think that makes sense. I''ll try that. Thanks.
I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it. -Jack Handy
your on the right track, but it''s not practical to RE-Allocate the memory before you change it. just make it big enough to hold all possible strings your would have at the begining.

wchar *str = new wchar[256];

wstrcpy(str,TEXT("Ham"));

wstrcpy(str,TEXT("Ham and Chicken"));

just remeber you have to free the memory at the end of the routines scope, with delete

delete [] str;


www.autodefrost.com/~travisSignature Virus cleaned by McAfee
Right. Thanks. It works now.
I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it. -Jack Handy

This topic is closed to new replies.

Advertisement