Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • 47
    Not that I dislike C, however strtok is not thread-safe, and you need to be certain that the string you send it contains a null character to avoid a possible buffer overflow.
    – tloach
    Commented May 10, 2010 at 13:18
  • 11
    There is strtok_r, but this was a C++ question. Commented Oct 6, 2010 at 9:14
  • 3
    @tloach: in MS C++ compiler strtok is thread safe as the internal static variable is created on the TLS (thread local storage) (actually it is compiler depended)
    – Ahmed
    Commented Nov 28, 2010 at 15:03
  • 5
    @ahmed: thread safe means more than just being able to run the function twice in different threads. In this case if the thread is modified while strtok is running it's possible to have the string be valid during the entire run of strtok, but strtok will still mess up because the string changed, it's now already past the null character, and it's going to keep reading memory until it either gets a security violation or finds a null character. This is a problem with the original C string functions, if you don't specify a length somewhere you run into problems.
    – tloach
    Commented Nov 29, 2010 at 13:23
  • 4
    strtok requires a pointer to a non-const null-terminated char array, which is not a common creature to find in c++ code ... what's your favourite way to convert to this from a std::string?
    – fuzzyTew
    Commented Aug 3, 2013 at 14:43