By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

strtok

P: n/a
The c99 standard states "The implementation shall behave as if no
library function calls the strtok function." What exactly does this
mean? Does this dictate any requirements to architectures supporting
proccesses or threads?
--
Zack
Mar 15 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Zack <goldszs...@gmail.comwrote:
The c99 standard states "The implementation shall behave as if
no library function calls the strtok function." What exactly
does this mean?
Exactly that. Because the strtok function has its own record
of the string location being scanned, you can't use it to scan
two strings simultaniously.

So, consider a loop that uses strtok to split a given string.
Now suppose that loop uses printf to print each 'token' as it
goes. If printf has the potential to use strtok internally,
then the user loop potentially crumbles into chaos because
printf will have clobbered or undermined strtok's record.
Does this dictate any requirements to architectures
supporting proccesses or threads?
No, it dictates library implementation requirements and gives
programmers greater freedom to mix strtok with other library
functions (e.g. other string library functions).

[Of course, if strtok were better designed, there wouldn't be
an issue.]

--
Peter

Mar 15 '07 #2

P: n/a
In article <h_******************************@comcast.com>,
Zack <go********@gmail.comwrote:
>The c99 standard states "The implementation shall behave as if no
library function calls the strtok function." What exactly does this
mean? Does this dictate any requirements to architectures supporting
proccesses or threads?
Imagine what would happen if your program looked like:

/* Don't need processes or threads for this to be a problem */
x = strtok(...); /* I'm using strtok, with its stateful nature */

y = somelibfunc(...); /* What would happen if somelibfunc called strtok
internally? */

Mar 15 '07 #3

P: n/a
Zack wrote:
>
The c99 standard states "The implementation shall behave as if no
library function calls the strtok function." What exactly does this
mean?
It means that if you call another library function
in between two strtok calls,
that it shouldn't affect the state of the strtok function.

--
pete
Mar 16 '07 #4

P: n/a
Peter Nilsson wrote:
No, it dictates library implementation requirements and gives
programmers greater freedom to mix strtok with other library
functions (e.g. other string library functions).
Okay,
I guess I'm getting OT now but if I have a proccess what is the scope of
strtok? Practically how do you deal with 3rd party libraries? If they
use strtok do you get hosed? The way I'm reading this is that if i link
to any libraries using strtok we can both get chaos mixed in. The only
exemption is std c library functions.
--
Zack
Mar 16 '07 #5

P: n/a
Zack wrote:
Peter Nilsson wrote:
>No, it dictates library implementation requirements and gives
programmers greater freedom to mix strtok with other library
functions (e.g. other string library functions).
Okay,
I guess I'm getting OT now but if I have a proccess what is the scope of
strtok? Practically how do you deal with 3rd party libraries? If they
use strtok do you get hosed? The way I'm reading this is that if i link
to any libraries using strtok we can both get chaos mixed in. The only
exemption is std c library functions.
You are getting OT. :-)

You have to make a distinction between processes and threads
depending on the platform. strtok will probably behave normally
if called from two separate processes at the same time but it
will probably not work correctly with threads. This is just a
hint, notice the two "probably". You'll have to ask in a more
appropriate group and check the documentation for your standard
libary.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Mar 16 '07 #6

P: n/a
Zack wrote:
Peter Nilsson wrote:
>No, it dictates library implementation requirements and gives
programmers greater freedom to mix strtok with other library
functions (e.g. other string library functions).

I guess I'm getting OT now but if I have a proccess what is the
scope of strtok? Practically how do you deal with 3rd party
libraries? If they use strtok do you get hosed? The way I'm
reading this is that if i link to any libraries using strtok we
can both get chaos mixed in. The only exemption is std c library
functions.
It'll work just fine, as long as you don't give other routines the
option of calling it while you want it to preserve status for you.
When you call it with a non-NULL pointer you initialize it.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 16 '07 #7

P: n/a
CBFalconer wrote:
Zack wrote:
>I guess I'm getting OT now but if I have a proccess what is the
scope of strtok? Practically how do you deal with 3rd party
libraries? If they use strtok do you get hosed? The way I'm
reading this is that if i link to any libraries using strtok we
can both get chaos mixed in. The only exemption is std c library
functions.

It'll work just fine, as long as you don't give other routines the
option of calling it while you want it to preserve status for you.
When you call it with a non-NULL pointer you initialize it.
strtok will work for me just fine if I link with a library that uses
strtok?

--
Zack
Mar 17 '07 #8

P: n/a
Zack wrote:
CBFalconer wrote:
>Zack wrote:
>>I guess I'm getting OT now but if I have a proccess what is the
scope of strtok? Practically how do you deal with 3rd party
libraries? If they use strtok do you get hosed? The way I'm
reading this is that if i link to any libraries using strtok we
can both get chaos mixed in. The only exemption is std c library
functions.

It'll work just fine, as long as you don't give other routines the
option of calling it while you want it to preserve status for you.
When you call it with a non-NULL pointer you initialize it.

strtok will work for me just fine if I link with a library that
uses strtok?
The linking doesn't matter. The calling does. Since the library
is a black box, don't call that library while you need the strtok
status preserved.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 17 '07 #9

P: n/a
CBFalconer wrote:
Zack wrote:
>CBFalconer wrote:
>>Zack wrote:

I guess I'm getting OT now but if I have a proccess what is the
scope of strtok? Practically how do you deal with 3rd party
libraries? If they use strtok do you get hosed? The way I'm
reading this is that if i link to any libraries using strtok we
can both get chaos mixed in. The only exemption is std c library
functions.

It'll work just fine, as long as you don't give other routines the
option of calling it while you want it to preserve status for you.
When you call it with a non-NULL pointer you initialize it.

strtok will work for me just fine if I link with a library that
uses strtok?

The linking doesn't matter. The calling does. Since the library
is a black box, don't call that library while you need the strtok
status preserved.
Ok that's what I thought. So if a library is treated as a black box
you have no idea whether strtok is safe or not.
Thanks.
--
Zack
Mar 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.