wcscpy and memcpy

A

Amon Tse

Dear All,

I am new to unicode developement in C++. Could anyone give me an idea
whether the following (1), (2) and (3) are equivalent and correct in
semantics:

wchar_t src[] = L'ABCDE';
wchar_t tgt[100];

wcscpy(tgt, src); //(1)

memcpy(tgt, src, wcslen(src)*sizeof(wchar_t)+sizeof(L'\0')); //(2)

memcpy(tgt, src, (wcslen(src)+1)*sizeof(wchar_t)); //(3)


Thanks in advance!
Amon
 
R

Ron Natalie

Amon said:
wchar_t src[] = L'ABCDE';
You want quotes (") here to make a string literal.
wcscpy(tgt, src); //(1)

If you're going to insist on using character pointers and arrays here
rather than being sensible and using std::wstring, this is probably the
best way.
memcpy(tgt, src, wcslen(src)*sizeof(wchar_t)+sizeof(L'\0')); //(2)

This will work, but it is:

1. Inefficient as wcscpy probably does a better job of combinging the
copying and looking for an end of string.

2. DANGEROUS if this code ever gets moved back to C. In C the sizeof
a character constant is always sizeof(int). In C++ it's the actual size
of the character.
memcpy(tgt, src, (wcslen(src)+1)*sizeof(wchar_t)); //(3)
See #1 above.
 

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

Similar Threads

memcpy() 3
wcslen function 2
Is this String class properly implemented? 96
silly with memcpy() 4
memcpy? 3
wide char 3
Help with Visual Lightbox: Scripts 2
SpiderMonkey Multithreading String Issues 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top