What's the difference between strtok and strtok_r in C?

后端 未结 4 1874
忘掉有多难
忘掉有多难 2021-02-13 04:29

What\'s the difference between strtok and strtok_r in C and when are we supposed to use which?

4条回答
  •  无人共我
    2021-02-13 05:10

    The _r versions of functions are reentrant: you can call them from multiple threads simultaneously, or in nested loops, et cetera. Reentrant versions usually take an extra argument, this argument is used to store state between calls instead of using a global variable.

    The non-reentrant versions often use global state, so if you call them from multiple threads, you are probably invoking undefined behavior. Your program could crash, or worse.

    From the man pages (man 3 strtok):

    The strtok_r() function is a reentrant version of strtok(). The context pointer last must be provided on each call. The strtok_r() function may also be used to nest two parsing loops within one another, as long as separate context pointers are used.

提交回复
热议问题