jalkadir wrote:
My test run compiles, but I get a segmentation fault.
gdb.exe reports
#0 0x77c1d2b9 in msvcrt!mblen
What am I doing wrong?
Any body!!
You don't understand pointers and arrays. You need to C++ text book.
#include <cstdlib>
#include <iostream>
#include <wchar.h>
using namespace std;
std::wstring ctow(const char* src){
wchar_t* dest;
Unitinialised pointer, you need to allocate some memory.
int i = mbstowcs(dest, src, sizeof(src));
sizeof(src) gives the sizeof the pointer (four bytes) not the size of
the string.
strlen(src) seems right.
It would be better like this, using a vector to allocate the necessary
memory.
std::wstring ctow(const char* src){
std::vector<wchar_t> dest(how_ever_many_bytes_you_need);
int i = mbstowcs(&dest[0], src, strlen(src));
return std::wstring(&dest[0]);
}
I don't know how you get the value of 'how_ever_many_bytes_you_need',
I'll leave you to work that out.
If you are a newbie, then don't use pointers when you don't have to,
especially when you don't understand them.
john