我可以调用的memcpy()和memmove与()与"字节&QUOT数;设定为零? [英] Can I call memcpy() and memmove() with "number of bytes" set to zero?

查看:142
本文介绍了我可以调用的memcpy()和memmove与()与"字节&QUOT数;设定为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要治疗的情况下,当我actully什么都没有移动/与 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 可以到该函数的调用零值。除非明确说明
  否则在一个特定的功能在本节中,指针参数说明
  这样的电话仍然应具有有效的值,如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与()与"字节&QUOT数;设定为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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