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

4

26

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

Linguini answered 5/3, 2014 at 22:15 Comment(3)
One is reentrant the other is not. I think the manual states that.Gretel
Not standard C, it is Posix. Strtok() is a bug factory, it uses a global variable inside the CRT to keep track of the string position. So you can't use it on multiple strings simultaneously. Also risky in threads, most CRTs do solve that.Astronomical
To confuse you even more, C11 adds strtok_s.Avis
A
29

strtok is equivalent to (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 rather than strtok, unless you need to make your code portable to pre-POSIX-2001 systems that only support strtok

Auster answered 5/3, 2014 at 23:13 Comment(0)
A
22

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.

Adalia answered 5/3, 2014 at 22:20 Comment(2)
Is there a list of such _r functions vs the original ones I may have used without knowing ? Since the _r are not standard C I can't just grep for them in *.hJocosity
@dargaud: They are in the *.h files. The functions are not “standard C” but they are part of the POSIX standard. The only other one I can think of is strerror_r.Adalia
F
6

strtok save static pointer for reuse in the next time, when you give NULL as the first parameter, so you just can't parse 2 strings in parallel.

In the strtok_r you give also the pointer, as out parameter (pointer to pointer). so there is no static pointer in the function and you can move from one string to another and back...

Fredenburg answered 5/3, 2014 at 22:24 Comment(0)
T
1

According 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 string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok_r() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter. The string of delimiters, s2, can differ from call to call.

Topee answered 5/3, 2014 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.