我可以用“字节数"调用 memcpy() 和 memmove() 吗?设置为零?

Can I call memcpy() and memmove() with quot;number of bytesquot; set to zero?(我可以用“字节数调用 memcpy() 和 memmove() 吗?设置为零?)
本文介绍了我可以用“字节数"调用 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() 吗?设置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Could I ever want to access the address zero?(我可以想访问地址零吗?)
C++ Access derived class member from base class pointer(C++ 从基类指针访问派生类成员)
Weird Behaviour with const_cast(const_cast 的奇怪行为)
Are pointer variables just integers with some operators or are they quot;symbolicquot;?(指针变量只是带有某些运算符的整数还是“符号?)
Modifying a char *const string(修改 char *const 字符串)
Modifying a const int in C++(修改 C++ 中的 const int)