Earlier quoted context omitted.
Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.
> Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it. Well, there is already a thread-safe variant [0]: > The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you. [0] https://linux.die.net/man/3/strtok_r
String tokenization in C
11–20 of 117 posts
Re: String tokenization in C
#12If you program in C please just write those four obvious lines yourself.
Re: String tokenization in C
#13It used to be that gcc will warn against strtok and recommend strsep instead. Do not know what the status is today
Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.
Re: String tokenization in C
#14strtok is one of the silliest parts of the standard library. (And there are many bad ones). It's broken. It's not thread safe (yes there is strtok_r). It's needlessly hard to use. And it writes zeros to the input array. The latter means it's unfit for most use cases, including non-trivial tokenization where you want e.g. to split "a+1" into three tokens. If you program in C please just write those four obvious lines…
libc has somehow managed to hit the sweet spot and have APIs that are both inconvenient to use properly, and perform poorly.
Re: String tokenization in C
#15Earlier quoted context omitted.
Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.
Well, strtok could use thread local variables to store intermediate state, to make it threadsafe while maintaining the same API. Not saying this is a good idea, but technically it would work, no?
Re: String tokenization in C
#16Note though that strsep() is not as portable, because it is an extension to standard C.
Re: String tokenization in C
#17 strcpy(str,"abc,def,ghi");
token = strtok(str,",");
printf("%s \n",token);
Even if the author knows how many tokens are returned I would prefer a check for NULL here since a good fraction might not read further than this bad example.Re: String tokenization in C
#18Well, yes, using strtok works if the data happens to be structured in a certain simple way. Very often you want to do something more advanced though, and using regex for matching tokens is then necessary.
Re: String tokenization in C
#19strtok is one of the silliest parts of the standard library. (And there are many bad ones). It's broken. It's not thread safe (yes there is strtok_r). It's needlessly hard to use. And it writes zeros to the input array. The latter means it's unfit for most use cases, including non-trivial tokenization where you want e.g. to split "a+1" into three tokens. If you program in C please just write those four obvious lines…
Much of libc is terrible from an API design perspective, even given the limitations of C as a language. libc has somehow managed to hit the sweet spot and have APIs that are both inconvenient to use properly, and perform poorly.
Re: String tokenization in C
#20Earlier quoted context omitted.
In fact, it's not even in POSIX .
To quote the GNU C library manual: “ This function was introduced in 4.3BSD and therefore is widely available. ”¹ 1. https://www.gnu.org/software/libc/manual/html_node/Finding-T...