简体   繁体   English

找不到strtok_s()的文档

[英]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: 作为安全功能的一部分检查的运行时约束为:

  • The pointers s1max , s2 , and ptr must all be non-null. 指针s1maxs2ptr必须全部为非空。
  • If s1 is a null pointer, then *ptr must not be. 如果s1为空指针,则*ptr一定不能为空。
  • The value of *s1max must be less than or equal to RSIZE_MAX . *s1max的值必须小于或等于RSIZE_MAX
  • The end of the token found must occur within the first *s1max characters of s1 for the first call 找到的令牌的结尾必须出现在首次调用的s1的前*s1max字符内
  • The end of the token found must occur within the first *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 . 安全方面是,如果违反了这些约束中的任何一个,则不会在s1s2上发生任何间接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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM