为什么不考虑将strnlen()纳入C23?

发布于 2025-02-01 09:10:05 字数 1415 浏览 2 评论 0 原文

功能 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城月光淡如水﹏ 2025-02-08 09:10:06

相互介绍,该功能是在

在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页。

由于没有共识而被拒绝。

6.33 Sebor,将Strnlen添加到C2X [N 2351]

...

*稻草民意调查:应该将N2351放入C2X?

(11/6/6)

尚不清楚共识。

结果,未添加功能。

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.

6.33 Sebor, Add strnlen to C2X [N 2351]

...

*Straw poll: Should N2351 be put into C2X?

(11/6/6)

Not clear consensus.

As result the function was not added.

永不分离 2025-02-08 09:10:06

反对 strnlen 的一个参数是它是一个多余的函数,因为我们已经具有 memchr 。示例:

const char str[666] = "hello world";
size_t length1 = strnlen(str,666);
size_t length2 = (char*)memchr(str,'\0',666) - str;

memchr 的优点:

  • 自时间的曙光以来已经是标准C功能。
  • 在某些情况下(?),可能比 strnlen 更有效。
  • 更多通用的API。
  • memchr 在调用 strcpy 之类的函数之前,应该为了对假定的字符串输入进行消毒,因此应该使用什么目的 strnlen 填充尚不清楚。
  • 具有适当的错误处理,与 strnlen 不同,该错误无法确定是否失败。

缺点:

  • 更尴尬和类型的不安全接口,目的是专门查找字符串长度。

One argument against strnlen is that it's a superfluous function, since we already have memchr. Example:

const char str[666] = "hello world";
size_t length1 = strnlen(str,666);
size_t length2 = (char*)memchr(str,'\0',666) - str;

Advantages of memchr:

  • Already been a standard C function since the dawn of time.
  • Possibly more efficient than strnlen in some situations(?).
  • More generic API.
  • memchr already ought to be in use for the purpose of sanitising supposed string input before calling functions like strcpy, so what purpose strnlen fills is unclear.
  • Has proper error handling, unlike strnlen which does not tell if it failed or not.

Disadvantages:

  • More awkward and type-unsafe interface for the purpose of finding a string length specifically.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文