I've tried implementing a function like this, but unfortunately it doesn't work:
const wchar_t *GetWC(const char *c) { const size_t cSize = strlen(c)+1; wchar_t wc[cSize]; mbstowcs (wc, c, cSize); return wc; }
My main goal here is to be able to integrate normal char strings in a Unicode application. Any advice you guys can offer is greatly appreciated.
The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.
Wide characters are similar to character datatype. The main difference is that char takes 1-byte space, but wide character takes 2-bytes (sometimes 4-byte depending on compiler) of space in memory. For 2-byte space wide character can hold 64K (65536) different characters. So the wide char can hold UNICODE characters.
In your example, wc
is a local variable which will be deallocated when the function call ends. This puts you into undefined behavior territory.
The simple fix is this:
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; }
Note that the calling code will then have to deallocate this memory, otherwise you will have a memory leak.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With