472,352 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

wcscpy and memcpy

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

Jul 23 '05 #1
1 4991
Amon Tse wrote:

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.
Jul 23 '05 #2

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

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by...
5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that...
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed...
16
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it...
6
by: MIUSS | last post by:
Hello I got an unusual problem and I don't know how to solve it out. I need to get success with the handle variable hFile. It's not problem...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.