Home > Article > Backend Development > How to Safely Convert char to wchar_t in a Unicode Application?
In a Unicode application, integrating standard char strings requires converting char to wchar_t. However, attempts to implement this conversion often encounter undefined behavior.
Consider the following function:
const wchar_t *GetWC(const char *c) { const size_t cSize = strlen(c)+1; wchar_t wc[cSize]; mbstowcs (wc, c, cSize); return wc; }
This function allocates an array of wchar_t, converts the char to wchar_t, and returns the pointer to wc. However, wc is a local variable that is deallocated at the end of the function, leading to undefined behavior when the returned pointer is dereferenced.
The error can be fixed by allocating the wchar_t* array dynamically within the function:
const wchar_t *GetWC(const char *c) { const size_t cSize = strlen(c)+1; wchar_t* wc = new wchar_t[cSize]; mbstowcs (wc, c, cSize); return wc; }
This dynamic allocation ensures that the memory will persist beyond the function's lifetime.
It's crucial to note that the caller of GetWC() is now responsible for deallocating the allocated memory. Failure to do so will result in a memory leak.
The above is the detailed content of How to Safely Convert char to wchar_t in a Unicode Application?. For more information, please follow other related articles on the PHP Chinese website!