Re: GUILD related layout

From: Mysidia (jmhess@i-55.com)
Date: 04/01/01


On Sun, 1 Apr 2001, Peter Ajamian wrote:

> >    There are very good reasons to _AVOID_ using strtok() which is really
> > a very nasty function.
>
> No just avoid using it incorrectly such as how you try to use it in your
> example.

I ignored the results, so what? It's very clear that `p' is the only
thing to hold the strtok(...) results in :P

The fact that code couldn't work (the values of strtok at the different
call levels would clobber each other), is the very flaw I speak of.

You can't safely call _ANY_ function and then later use strtok with the
first pointer NULL to iterate the thing you were last working with.. unless
you know with 100% certainty that the function you are calling does not
strtok and will never should strtok no matter how greatly someone
bastardizes it.

There are better devices than the ol' dangerous strtok...

From the NetBSD Programmer's Manual:
STRTOK(3)                 NetBSD Programmer's Manual             STRTOK(3)

DESCRIPTION
     This interface is obsoleted by strsep(3).
[...]
STANDARDS
     The strtok() function conforms to ANSI X3.159-1989 (``ANSI C'').  The
     strtok_r() function conforms to IEEE Std1003.1c-1995 (``POSIX'').
BUGS
     There is no way to get tokens from multiple strings simultaneously.

     The System V strtok(), if handed a string containing only delimiter
     characters, will not alter the next starting point, so that a call to
     strtok() with a different (or empty) delimiter string may return a
     non-NULL value.  Since this implementation always alters the next
     starting  point, such a sequence of calls would always return NULL.

[...]
From the Linux programmer's manual:

STRTOK(3)           Linux Programmer's Manual           STRTOK(3)
[...]
BUGS
       Never use this function.  This function modifies its first
       argument.   The  identity  of  the delimiting character is
       lost.  This function cannot be used on constant string

from SunOS' manual pages:
Standard C Library Functions                           string(3C)
[...]
     The strtok() function is Unsafe  in  multithreaded  applica-
     tions.  The strtok_r() function should be used instead.
[...]


> No strtok() is not reentrant in this fashion, of course, never once have
> I come across a situation where I really wanted to use strtok() in such a
> manner.  Your example also shows that you really don't know the slightest

  The point isn't that you would want to use it in this matter; the point
is, someone implementing a function dispatched from your little parser
function might decide to strtok() without exactly realizing the
consequences of their action or the side effects it would have on the
parent function (probably causing them to attempt to use data that no
longer exists -- unless you insanely use a global or static)

> thing about how strtok() works.  The compilation would never get past the
> first the strtok() line in your example and even if it does it will not
> work anything like what you're expecting.

No, it would compile fine; there are no syntax errors in it -- despite
the fact that it's not meant to be compiled

> No function will do what you want it to do if you use it incorrectly.
To use the strtok function correctly it can't be called from within
a child, this is a defect in the entire design of the thing.

> So?  Don't use it for recursion then, or do, there _are_ ways to use
> strtok() in a recursive manner which would work.  As long as you're aware
> of how strtok() works and what your coding will do with it.

You don't necessarily know what functions your calling do with things;
you may later reimplement them, and you might want to use tokenization in
THEM.

It's not that it's just recursion that causes problems; there
are other ways: ie: calling any function from a strtok loop if that
function uses strtok at well -- or calling any function that uses strtok
from a function called from a strtok loop.

You may be potentially calling a function that does strtok() without
even knowing it and ruining your data, randomly -- maybe you were
just so absent-minded you didn't want to comb through every function
you're calling (and every function they call) for a little strtok() here
or there.

>   for (p1 = strtok(buf, " "), p2 = strtok(NULL, "");
>        p1; p1 = strtok(p2, " "), p2 = strtok(NULL, "")) {
>     printf("%s "strtok(p1, "-"));
>  }
> }
That's an iterative loop, not a recursion..

> Here is the output this little function would give...
>
> Th is a t
I would expect that it would have an " est." in there somewhere as well..

fin.

-Mysid

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST