Incorrect use of mbstowcs?

  • Thread starter Ralph A. Moritz
  • Start date
R

Ralph A. Moritz

Hi all,

my knowledge of character encoding issues is pretty limited and I
simply
cannot find a problem with the following code. It compiles -- but
seems to
produce garbage at runtime. I'd appreciate any helpful suggestions.

wchar_t* itemNameW = new wchar_t[strlen(itemName + 1) *
sizeof(wchar_t)];
mbstowcs(itemNameW, itemName, strlen(itemName) +1);

TIA,
Ralph
 
A

Alf P. Steinbach

* Ralph A. Moritz:
Hi all,

my knowledge of character encoding issues is pretty limited and I
simply
cannot find a problem with the following code. It compiles -- but
seems to
produce garbage at runtime. I'd appreciate any helpful suggestions.

wchar_t* itemNameW = new wchar_t[strlen(itemName + 1) *
sizeof(wchar_t)];

itemName+1 is a pointer to the second character of itemName. Instead of
the length + 1 you therefore get the length - 1.

mbstowcs(itemNameW, itemName, strlen(itemName) +1);

You're not copying the terminating null-character.
 
A

Alf P. Steinbach

* Ralph A. Moritz:
Hi all,

my knowledge of character encoding issues is pretty limited and I
simply
cannot find a problem with the following code. It compiles -- but
seems to
produce garbage at runtime. I'd appreciate any helpful suggestions.

wchar_t* itemNameW = new wchar_t[strlen(itemName + 1) *
sizeof(wchar_t)];

Oh sorry, I didn't see that there was even more, due to the
line-breaking: don't multiply by sizeof(wchar_t).
 
A

Alf P. Steinbach

* Ralph A. Moritz:
Hi all,

my knowledge of character encoding issues is pretty limited and I
simply
cannot find a problem with the following code. It compiles -- but
seems to
produce garbage at runtime. I'd appreciate any helpful suggestions.

wchar_t* itemNameW = new wchar_t[strlen(itemName + 1) *
sizeof(wchar_t)];
mbstowcs(itemNameW, itemName, strlen(itemName) +1);

Even more sorry, forgot to give the standard advice (which you should
heed): use std::wstring, forget about that manual memory management.
 
R

Ron Natalie

Ralph said:
Hi all,

my knowledge of character encoding issues is pretty limited and I
simply
cannot find a problem with the following code. It compiles -- but
seems to
produce garbage at runtime. I'd appreciate any helpful suggestions.

wchar_t* itemNameW = new wchar_t[strlen(itemName + 1) *
sizeof(wchar_t)];

new already handles the size of the object. There's no need to
multiply by wchar_t. Strlen is already going to give you a pessimistic
view of the number of wchar's required.
mbstowcs(itemNameW, itemName, strlen(itemName) +1);
So what's in itemName?

What did you do to examine itemNameW to determine it's validity?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top