strtok and strtok_r

S

siddhu

Dear experts,

As I know strtok_r is re-entrant version of strtok.
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?

Regards,
Siddharth
 
V

Victor Bazarov

siddhu said:
Dear experts,

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.
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?

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?

V
 
S

siddhu

I'll take your word for it. There is no 'strtok_r' in the Standard
C++ library.


I am wondering that maybe the term "reentrant" is misused here. Do
you mean "multithreaded"?
Multithreaded or in case of signal handling. We expect function to be
reentrant in these cases.
 
P

Puppet_Sock

As I know strtok_r is re-entrant version of strtok.

The purpose of strtok is to teach people the value of
proper design by catching their gonads in the tractor
gears every time they use it.

If you really need a tokenizer, build one from the std
containers. It's not that hard. If you want to do it for
c-string char* type data, it's pretty easy. Put it in a
class then make an object to do each independent string
you need to tokenize. Or have the tokenized data come
back in a std::vector<std::string> of data.
Socks
 
J

James Kanze

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.

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).
 
J

James Kanze

[snip]
As I know strtok_r is re-entrant version of strtok.
The purpose of strtok is to teach people the value of
proper design by catching their gonads in the tractor
gears every time they use it.

Finally someone who has found a use for it. (I know that I've
never used it. And I'm tokenizing strings all the time. It has
to be one of the worse functions ever specified, not far behind
gets().)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top