功能 strdup()
和 strndup()
最终将其纳入即将到来的C23标准:
7.24.6.4 strdup
函数
概要
#include< string.h>
char *strdup(const char *s);
strdup
函数在分配的空间中创建 s
指向的字符串副本,好像是通过调用 malloc
。 /p>
返回
strdup
函数返回指向重复字符串的第一个字符。返回的指针可以传递给免费
。如果无法分配空间,则 strdup
函数返回零指针。
7.24.6.5 strndup
函数
概要
#include< string.h>
char *strndup(const char *s,size_t size);
strndup
函数创建一个初始化的字符串,不超过 size
数组的初始字符,指向 s
,直到第一个字符null字符,以先到者为准,在分配的空间中,好像是通过呼叫 malloc
。如果数组指向 s
在第一个 size
字符中不包含null,则将null附加到数组的副本中。
返回
strndup
函数返回指向创建字符串的第一个字符的指针。返回的指针可以传递给免费
。如果无法分配空间,则 strndup
函数返回零指针。
为什么不考虑包含POSIX-2008函数 strnlen
?
#include <string.h>
size_t strnlen(const char *s, size_t maxlen);
strnlen()
函数应计算 s
点的数组中的较小字节数,而不包括终止的nul cartarair或 Maxlen 参数。 strnlen()
函数永远不会比 maxlen
字节 by s
字节。
Functions strdup()
and strndup()
have finally made it into the upcoming C23 Standard:
7.24.6.4 The strdup
function
Synopsis
#include <string.h>
char *strdup(const char *s);
The strdup
function creates a copy of the string pointed to by s
in a space allocated as if by a call to malloc
.
Returns
The strdup
function returns a pointer to the first character of the duplicate string. The returned pointer can be passed to free
. If no space can be allocated the strdup
function returns a null pointer.
7.24.6.5 The strndup
function
Synopsis
#include <string.h>
char *strndup(const char *s, size_t size);
The strndup
function creates a string initialized with no more than size
initial characters of the array pointed to by s
and up to the first null character, whichever comes first, in a space allocated as if by a call to malloc
. If the array pointed to by s
does not contain a null within the first size
characters, a null is appended to the copy of the array.
Returns
The strndup
function returns a pointer to the first character of the created string. The returned pointer can be passed to free
. If no space can be allocated the strndup
function returns a null pointer.
Why was the POSIX-2008 function strnlen
not considered for inclusion?
#include <string.h>
size_t strnlen(const char *s, size_t maxlen);
The strnlen()
function shall compute the smaller of the number of bytes in the array to which s
points, not including the terminating NUL character, or the value of the maxlen
argument. The strnlen()
function shall never examine more than maxlen
bytes of the array pointed to by s
.
发布评论
评论(2)
相互介绍,该功能是在
在2019年的伦敦会议上进行了讨论。请参阅议程:
https:// https://wwwww9.open-std.org/jtc1 /sc22/wg14/www/docs/n2370.htm
可以在 https://www9.open-std.org/jtc1/sc22/wg14/wwww/docs/n2377.pdf 。
第59页。
由于没有共识而被拒绝。
结果,未添加功能。
Interesingly, this function was proposed in https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2351.htm
It was discussed at the London meeting in 2019. See the agenda:
https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2370.htm
The discussion minutes can be found at https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2377.pdf.
Page 59.
It was rejected due to no consensus.
As result the function was not added.
反对
strnlen
的一个参数是它是一个多余的函数,因为我们已经具有memchr
。示例:memchr
的优点:strnlen
更有效。memchr
在调用strcpy
之类的函数之前,应该为了对假定的字符串输入进行消毒,因此应该使用什么目的strnlen
填充尚不清楚。strnlen
不同,该错误无法确定是否失败。缺点:
One argument against
strnlen
is that it's a superfluous function, since we already havememchr
. Example:Advantages of
memchr
:strnlen
in some situations(?).memchr
already ought to be in use for the purpose of sanitising supposed string input before calling functions likestrcpy
, so what purposestrnlen
fills is unclear.strnlen
which does not tell if it failed or not.Disadvantages: