What is the difference between strtok and strtok_r in C?

What is the difference between strtok and strtok_r in C and when should we use which?

+8
c strtok
source share
4 answers

strtok equivalent (and often defined as):

 char *strtok(char *str, const char *delim) { static char *save; return strtok_r(str, delim, &save); } 

in general, you should use strtok_r directly, not strtok , unless you need to port your code to systems prior to POSIX-2001 that only support strtok

+15
source share

Functions of _r functions are reentrant: you can call them from several threads at the same time or in nested loops, etc. In reentrant versions, an optional argument is usually used; this argument is used to store state between calls instead of using a global variable.

Non-reentrant versions often use global state, so if you call them from multiple threads, you are likely to call undefined behavior. Your program may crash or worse.

On the man pages ( man 3 strtok ):

The strtok_r() function is a reentrant version of strtok() . A context pointer last must be provided for each call. The strtok_r() function can also be used to nest two pairs of parsing within each other while separate context pointers are used.

+10
source share

strtok save a static pointer for reuse the next time you give NULL as the first parameter, so that you simply cannot split the two lines in parallel.

In strtok_r you also specify a pointer as the out parameter (pointer to a pointer). therefore, there is no static pointer in the function, and you can move from one line to another and back ...

+4
source share

According to the documentation, the strtok_r () function is a reentrant version of strtok ().

 char *strtok_r(char *s1, const char *s2, char **s3); 

It gets the next token from line s1, where tokens are lines separated by characters from s2. To get the first token from s1, strtok_r () is called with s1 as the first parameter. The remaining tokens from s1 are obtained by calling strtok_r () with a null pointer for the first parameter. The separator string s2 may differ from call to call.

0
source share

All Articles