[英]Can't find documentation for strtok_s()
What exactly is each argument passed to it, what does it return? 每个参数到底传递给它什么,它返回什么?
How exactly is it better than the regular strtok
? 它比常规
strtok
到底好多少?
Please give me the most simplified and basic explanation possible. 请给我最简单和基本的解释。
This is detailed in Appendix K (bounds checking interfaces)
of the ISO C11
standard. 这在
ISO C11
标准的Appendix K (bounds checking interfaces)
中有详细说明。 This optional part of the standard provides "safer" (a) versions of already existing functionality provided in the core part of the standard. 标准的此可选部分提供了标准核心部分中已提供的“更安全” (a)版本的已存在功能。
The prototype is: 原型是:
char *strtok_s (
char * restrict s1,
rsize_t * restrict s1max,
const char * restrict s2,
char ** restrict ptr);
The runtime constraints that are checked as part of the safety features are: 作为安全功能的一部分检查的运行时约束为:
s1max
, s2
, and ptr
must all be non-null. s1max
, s2
和ptr
必须全部为非空。 s1
is a null pointer, then *ptr
must not be. s1
为空指针,则*ptr
一定不能为空。 *s1max
must be less than or equal to RSIZE_MAX
. *s1max
的值必须小于或等于RSIZE_MAX
。 *s1max
characters of s1
for the first call s1
的前*s1max
字符内 *s1max
characters of where searching resumes on subsequent calls. *s1max
字符内。 The safety aspect is that, if any of those constraints are violated, no indirection occurs on s1
or s2
and no value is stored via ptr
. 安全方面是,如果违反了这些约束中的任何一个,则不会在
s1
或s2
上发生任何间接s2
并且不会通过ptr
存储任何值。
Other than those extra checks, it pretty much works identically to the standard strtok
function, returning tokens from s1
separated by groups of delimiters found in s2
. 除了这些额外的检查,它几乎与标准
strtok
函数相同,它们从s1
返回令牌,并由s2
的定界符组分隔。 I think the use of ptr
makes it thread-safe since it using a non-static state provided by the user (b) . 我认为
ptr
的使用使其具有线程安全性,因为它使用了用户(b)提供的非静态状态。
(a) Quoted because most parts of the standard are already safe if you know how to use them properly :-) (a)引用是因为如果您知道如何正确使用它们,该标准的大多数部分已经是安全的:-)
(b) The one thing that's still missing is the ability to have empty tokens such as: (b) 仍然缺少的一件事是具有空令牌的能力,例如:
field1||||field5
Because strtok_s
(and the original) treat ||||
因为
strtok_s
(和原始的)对待||||
as a single separator, we have to find other ways to do this </rant>
:-) 作为单个分隔符,我们必须找到其他方法来完成此操作
</rant>
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.