Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: strtok() and the mistery of the same address strings with different lengths... Date: Sat, 14 Jan 2017 14:27:44 -0800 Organization: None to speak of Lines: 27 Message-ID: References: <94292e94-bcb9-40ea-8b8a-2a8563eeb33e@googlegroups.com> <77fd1c6c-16e6-418f-859e-a56547a59389@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="ba0db46df61d502ee0e6afe10bb5a782"; logging-data="13715"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CORMx6DjtJLC0VlNkp3qD" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:33j7iQOBUXcgJlfcFHxjhKNvnz8= sha1:VOmdEU1/MpDLy7O7q84PvlByRTo= Xref: csiph.com comp.lang.c:101815 James Kuyper writes: [...] > Keep in mind that the same features that make strtok() problematic in > multi-threaded contexts also make it problematic in a far more ordinary > cases, one of which I actually ran into. The calling function was doing > a strtok() loop, and during each pass through the loop, it called a > subroutine. That subroutine was in a library I inherited responsibility > for, which also called strtok() for it's own purposes, rather thoroughly > messing up the calling routine. > I re-wrote the library function to do the same thing that strtok does, > but with explicit code rather than by calling strtok(). Perhaps a more straightforward example: Suppose you want to parse a string with strtok(), where each substring needs to be parsed with strtok(). For example, you might have several space-delimited fields, where each field contains several comma-delimited subfields. You can't use strtok() while another sequence of strtok() calls is pending. It saves a static pointer into the string passed on the first call -- and that pointer is shared by *all* calls to strtok(), not just calls in a particular parsed string. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"