6

This question applies both to C and C++.

memcpy basically copies raw memory from an address to another address. So my question is: what's the point of wmemcpy?

I mean, it's still contiguous space, and copying it is still the same process. It shouldn't matter if it's made of up wchar_t's, or should it?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
DeiDei
  • 10,205
  • 6
  • 55
  • 80

2 Answers2

4

From MSDN documentation of wmemcpy:

memcpy copies count bytes from src to dest; wmemcpy copies count wide characters (two bytes).

So the difference is on how many bytes will be copied with the given same arguments,when you say:

memcpy(src,dest,2);//2 bytes will be copied
wmemcpy(src,dest,2);//4 bytes,i.e 2*2 bytes will be copied

Other than this difference and possible convenience of use when using wmemcpy when copying wchar_t arrays, i don't think there is a difference between the two and the existence of wmemcpy is really necessary.

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
  • 1
    So that means `wmemcpy(src,dest,2);` is equivalent to `memcpy(src,dest,2*sizeof(wchar_t));`. It's just a convenience function? – DeiDei Apr 30 '16 at 19:25
  • @DeiDei I believe it is, since it copies raw bytes from source to destination. – Biruk Abebe Apr 30 '16 at 19:33
  • Well yes, that's what it *does*, but that doesn't explain why it makes sense to have `wmemcpy()` when there is already `memcpy()`. – John Bollinger Apr 30 '16 at 19:47
  • 1
    @John Bollinger Convenience of use may be? It would be rather convenient to say `wmemcpy(src,dest,2)` to copy two `wchar_t` arrays than saying `memcpy((char*)src,(char*)dest,2*sizeof(wchar_t))`. Don't you think so? – Biruk Abebe Apr 30 '16 at 19:50
  • 1
    Downvoted: Does not answer the question. – fuz Apr 30 '16 at 19:53
  • @FUZxxI Should i add my last comment to the answer then? – Biruk Abebe Apr 30 '16 at 19:54
  • No, actually, I don't think convenience provides much justification for this function. Or at least, I don't see what's so special about `wchar_t` that warrants it having its own special version of `memcpy()`, when none of C's base types -- not even `char` -- has one. – John Bollinger Apr 30 '16 at 19:55
  • @bkVnet “may be” is not an explanation. An explanation would cite an authoritative document or be extremely convincing. – fuz May 02 '16 at 16:17
  • @FUZxxI No it is not, it's just my opinion on the matter, as you have yours. That is why i use the word *"...possible convenience.."* in the answer. You have the right to disagree, as do i. – Biruk Abebe May 02 '16 at 17:13
3

Interfac will ensure you copied whole wchar_t number of symbols. You won't be able to copy odd number of bytes

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64