strncmp(NULL, "foo", 0) 定义明确吗?
Is strncmp(NULL, "foo", 0) well defined?
如果第三个参数为零,将 NULL 指针作为 strncmp
的参数是否安全? IE。像这样的调用:
strncmp(NULL, "foo", 0);
来自the C strncmp
documentation at cppreference.com:
The behavior is undefined when either lhs
or rhs
is the null pointer.
只需阅读文档即可。
C 标准说你不应该将 无效 指针传递给库函数,一般来说。
引用 C11
,章节 §7.24.1,"String function conventions",(强调我的)
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.
而且我在 7.24.4.4 中没有看到任何具体提及(作为上述约束的例外),strncmp()
函数。
为 "invalid pointers" 添加上下文,引用 §7.1.4/p1,库函数的使用
[...] If an argument to a function has an invalid value (such as a value
outside the domain of the function, or a pointer outside the address space of the program,
or a null pointer, or a pointer to non-modifiable storage when the corresponding
parameter is not const-qualified) or a type (after promotion) not expected by a function
with variable number of arguments, the behavior is undefined. [...]
关于NULL
,引用§7.19,<stddef.h>
NULL
which expands to an implementation-defined null pointer constant; [...]
如果第三个参数为零,将 NULL 指针作为 strncmp
的参数是否安全? IE。像这样的调用:
strncmp(NULL, "foo", 0);
来自the C strncmp
documentation at cppreference.com:
The behavior is undefined when either
lhs
orrhs
is the null pointer.
只需阅读文档即可。
C 标准说你不应该将 无效 指针传递给库函数,一般来说。
引用 C11
,章节 §7.24.1,"String function conventions",(强调我的)
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.
而且我在 7.24.4.4 中没有看到任何具体提及(作为上述约束的例外),strncmp()
函数。
为 "invalid pointers" 添加上下文,引用 §7.1.4/p1,库函数的使用
[...] If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined. [...]
关于NULL
,引用§7.19,<stddef.h>
NULL
which expands to an implementation-defined null pointer constant; [...]