473,468 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Incorrect use of mbstowcs?

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

Feb 5 '07 #1
4 2221
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 5 '07 #2
* 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).

mbstowcs(itemNameW, itemName, strlen(itemName) +1);
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 5 '07 #3
* 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 5 '07 #4
Ralph A. Moritz wrote:
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?
Feb 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michelle Hillard | last post by:
Hi guys, would appreciate if you can shed some light on this. Sorry to be a pain, can you tell me what is wrong with the following: for /F %%i in ('dir /b /on c:\bcp\pc*.txt') do bcp...
2
by: Linda Wienholt | last post by:
I have two usercontrols on the home page of my website. They are intermitently sending incorrect HTML to the browser, which results in a textbox being rendered with the wrong width. Either both...
1
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I get...
3
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I...
4
by: Peter Ritchie | last post by:
Does anyone know how to suppress a specific warning for a line or block of code in C#? C++ has a nice facility to disable a warning for a block of code with #pragma for warnings that are incorrect...
6
by: ypjofficial | last post by:
HI, I have following terrific confusion. class test { public: int i; int * j; int **k;
4
by: Kelvin Moss | last post by:
Hi all, I am trying to search within wide strings (unicode characters) using wcsstr (on Unix). My problem is that my src or dest strings may or may not be wide strings. The code I have written...
1
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.