我可以用“字节数"调用 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).
相关文章