[RFC] Should strtok_r be preferred to strtok?

Nathan Myers ncm-nospam@cantrip.org
Mon Nov 25 13:36:00 GMT 2002


On Mon, Nov 25, 2002 at 09:33:18PM +0100, Paolo Carlini wrote:
> Wolfgang Bangerth, in private mail, pointed my attention to the the 
> issue of thread-safety for strtok(), which I blatantly used in the 
> rewrite of locale::_Impl::_Impl(const char* __s, size_t __refs) for 
> thread safe :-( Posix localenames...
> 
> Should we autoconf for strtok_r and prefer it when available?

There is no good reason to use any of these functions.  The amount
of code in them is less than what you need to write yourself to use 
them safely.  In other words, you're better off re-implementing them
the right way.  (Hint: instead of poking out the delimiter with a
NUL character as a terminator, how about using begin/end pointer
ranges, like a good C++ coder?)

No sarcasm intended.  Thanks, Paolo, for giving me a nice opportunity
to vent on one of the most despicable bits of C89.

Nathan Myers
ncm-nospam@cantrip.org



More information about the Libstdc++ mailing list