問題描述
當我實際上沒有什么可移動/復制的情況時,我是否需要將 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() 嗎?設置為零?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!