为什么 strnlen() 不考虑包含在 C23 中?

chq*_*lie 4 c c-strings language-lawyer c23

功能strdup()strndup()最终纳入即将推出的 C23 标准:

7.24.6.4strdup函数

概要

#include <string.h>
char *strdup(const char *s);
Run Code Online (Sandbox Code Playgroud)

strdup函数在分配的空间中创建由 指向的字符串的副本s,就像通过调用 一样malloc

返回
strdup函数返回一个指向重复字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strdup函数返回空指针。

7.24.6.5strndup函数

概要

#include <string.h>
char *strndup(const char *s, size_t size);
Run Code Online (Sandbox Code Playgroud)

strndup函数创建一个字符串,该字符串初始化为不超过size由 指向的数组的初始字符s,直到第一个空字符(以先到者为准),在分配的空间中,就像调用 一样malloc。如果 by 指向的数组的s第一个字符中不包含 null size,则将 null 附加到数组的副本中。

返回
strndup函数返回一个指向所创建字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strndup函数返回空指针。

strnlen为什么不考虑包含POSIX-2008 函数?

#include <string.h>
size_t strnlen(const char *s, size_t maxlen);
Run Code Online (Sandbox Code Playgroud)

strnlen()函数应计算数组中指向的字节数s(不包括终止 NUL 字符)或参数值中的较小者maxlen。该strnlen()函数永远不会检查超过maxlen指向的数组的字节s

tst*_*isl 5

有趣的是,这个函数是在https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2351.htm中提出的

2019 年伦敦会议对此进行了讨论。议程参见: https ://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2370.htm

讨论纪要可在https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2377.pdf中找到。第 59 页。

由于没有达成共识而被拒绝。

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

...

*Straw poll:N2351是否应该放入C2X?

(11/6/6)

尚无明确共识。

结果这个功能没有被添加。