为什么wmemcpy时存在的memcpy应该足够了? [英] Why does wmemcpy exist when memcpy should suffice?

查看:1497
本文介绍了为什么wmemcpy时存在的memcpy应该足够了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wmemcpy 似乎执行相同的操作为的memcpy ,但接受 wchar_t的* 而不是无效* 。它的存在理由多么正当,如果这两个code段应具有相同的行为呢?它有一个有用的目的?

 的memcpy(DEST,wchar_array,sizeof的(wchar_array));

  wmemcpy(DEST,wchar_array,sizeof的(wchar_array)/的sizeof(wchar_t的));


解决方案

在除了davmac的有关API的对称性和答案有一个数组的大小并不总是理所当然的,应当强调的是,第三个参数 wmemcpy 是指元素复制(而不是字节)的数量。

如果您 wchar_t的对象的工作,并与其他功能的处理它们从< wchar.h> ,它可能有利于事宜。 wcslen 例如返回 wchar_t的元素中拥有C的宽字符串的长度,和 wcschr wcsrchr 收益 wchar_t的* ,从而用它们做一些指针运算,也可以保持你的数的元素的境界。

P.S。如果给出的最小 wchar_t的数组的大小意味着在你的榜样,使用 wmemcpy 可能会导致更优雅$ C $ç比的sizeof(wchar_array)您使用的:

 的#define SIZE 40
wchar_t的wchar_array [SIZE]
// ...
wmemcpy(DEST,wchar_array,SIZE);

wmemcpy appears to perform the same operations as memcpy but accepts wchar_t* instead of void*. How is its existence justified if these two code snippets should have the same behaviour? Does it have a useful purpose?

memcpy(dest, wchar_array, sizeof(wchar_array));

And

wmemcpy(dest, wchar_array, sizeof(wchar_array) / sizeof(wchar_t));

解决方案

In addition to davmac's answer about API symmetry and having the size of an array not always granted, it should be emphasized that the third argument of wmemcpy refers to number of elements to copy (rather than bytes).

If you work with wchar_t objects and handle them with other functions from <wchar.h>, it may facilitate matters. wcslen for instance returns the C wide string length in terms of wchar_t elements, and wcschr and wcsrchr return wchar_t *, thus using them to do some pointer arithmetic also keeps you in the "realm" of number-of-elements.

P.S. If the size of the minimal wchar_t array is given as implied in your example, using wmemcpy may result in more elegant code than that sizeof(wchar_array) you used:

#define SIZE 40
wchar_t wchar_array[SIZE];
// ...
wmemcpy(dest, wchar_array, SIZE);

这篇关于为什么wmemcpy时存在的memcpy应该足够了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆