Can I call memcpy() and memmove() with quot;number of bytesquot; set to zero?(我可以用“字节数调用 memcpy() 和 memmove() 吗?设置为零?)
问题描述
当我实际上没有什么可移动/复制的情况时,我是否需要将 memmove()
/memcpy()
作为边缘情况处理
Do I need to treat cases when I actully have nothing to move/copy with memmove()
/memcpy()
as edge cases
int numberOfBytes = ...
if( numberOfBytes != 0 ) {
memmove( dest, source, numberOfBytes );
}
或者我应该直接调用函数而不检查
or should I just call the function without checking
int numberOfBytes = ...
memmove( dest, source, numberOfBytes );
是否需要检查之前的代码片段?
Is the check in the former snippet necessary?
推荐答案
来自 C99 标准 (7.21.1/2):
From the C99 standard (7.21.1/2):
声明为 size_t n
的参数指定数组的长度函数,n
在调用该函数时可以具有零值.除非明确说明否则在本小节中对特定函数的描述中,指针参数此类调用仍应具有有效值,如 7.1.4 中所述.在这样的电话中,一个定位一个字符的函数没有找到,一个比较两个字符的函数字符序列返回零,复制字符的函数复制零字符.
Where an argument declared as
size_t n
specifies the length of the array for a function,n
can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function that compares two character sequences returns zero, and a function that copies characters copies zero characters.
所以答案是否定的;不需要检查(或者是;您可以通过零).
So the answer is no; the check is not necessary (or yes; you can pass zero).
这篇关于我可以用“字节数"调用 memcpy() 和 memmove() 吗?设置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以用“字节数"调用 memcpy() 和 memmove() 吗


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01