Re: strtok and strtok_r
On Sep 14, 5:16 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
siddhu wrote:
As I know strtok_r is re-entrant version of strtok.
I'll take your word for it. There is no 'strtok_r' in the Standard
C++ library.
It's Posix:-). Which isn't really on topic here. Any further
questions he might have concerning strtok_r should be addressed
to a Unix group.
strtok_r() is called with s1(lets say) as its first parameter.
Remaining tokens from s1 are obtained by calling strtok_r() with a
null pointer for the first parameter.
My confusion is that this behavior is same as strtok. So I assume
strtok_r must also be using any function static variable to keep the
information about s1. If this is the case then how strtok_r is re-
entrant?
Otherwise how it keeps the information about s1?
The obvious answer, whenever a Posix function is involved, is
man. As with all of the ..._r functions, Posix adds additional
arguments for pointers to what was the static data. On the
other hand, even strtok_r modifies the string it is passed as
the first parameter.
I am wondering that maybe the term "reentrant" is misused here. Do
you mean "multithreaded"? The the "static variable" could easily
be allocated one per thread, no?
The term in the Posix standard is reentrant; presumably, you can
call the function in a signal handler as well (although I have
my doubts about the portability of that, even under Posix). And
when Posix says that the function is "re-entrant", what it
really means is that it off loads the problem of reentrance to
the caller; it's up to the caller to ensure that all of the out
or inout arguments are accessed only from the given thread (or
invocation).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34