gpt4 book ai didi

c++ - 我可以调用 memcpy() 和 memmove() 并将 "number of bytes"设置为零吗?

转载 作者:IT老高 更新时间:2023-10-28 11:59:25 27 4
gpt4 key购买 nike

当我实际上没有东西可以移动/复制时,我是否需要处理将 memmove()/memcpy() 作为边缘情况的情况

int numberOfBytes = ...
if( numberOfBytes != 0 ) {
memmove( dest, source, numberOfBytes );
}

或者我应该直接调用函数而不检查

int numberOfBytes = ...
memmove( dest, source, numberOfBytes );

是否需要检查前一个片段?

最佳答案

来自 C99 标准 (7.21.1/2):

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.

所以答案是否定的;检查是没有必要的(或者是的;你可以通过零)。

关于c++ - 我可以调用 memcpy() 和 memmove() 并将 "number of bytes"设置为零吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com