Join Nostr
2024-09-03 04:39:09 UTC
in reply to

gentoobro on Nostr: Yes. There's basically no reason to use strtok ever. You can't (shouldn't) modify ...

Yes. There's basically no reason to use strtok ever. You can't (shouldn't) modify string literals, so it's possible that you could segfault if a pointer to a string literal got passed to strtok, depending on your system and if it locks the section containing the memory for string literals. Also, if I recall, it's not reentrant.

A basic loop with strchr can replace it, though in modern not-super-performance-sensitive code you really should just write a function like char\*\* strsplit(char\* in, char\* sep) that returns a malloc'd, null-terminated list of pointers to malloc'd null-terminated strings. Memory allocation is not the super slow bugbear it used to be.