Re: GUILD related layout

From: Peter Ajamian (peter@pajamian.dhs.org)
Date: 04/01/01


Mysidia wrote:
>
> On Sat, 31 Mar 2001, Peter Ajamian wrote:
>
> > > db.c:
>
> > Huh?!?  I think you meant to enclose that with an if () statement or
> > something.  At any rate, you have a comparison that does nothing and
> > you're missing a ; here.  Also, I always preferred using strtok()/atoi()
> > to parse input lines.  It allows you to parse them a little at a time so
> > you don't have to allocate an array like numbers[] to store the whole
> > thing at once.
>
>    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.

> Because of its use of static buffers, you can't
> use strtok() parsing within a function called by strtok() parsing.

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

<snip bad uasage example of strtok()>

> Would _NOT_ do what you want it to do.

No function will do what you want it to do if you use it incorrectly.

> For this reason, and the fact that strtok() is not even recursion safe,

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.

> strtok() is a thing that should never be used haphazardly because it is
> prone to causing future troubles -- the exception is for example if you
> have 'command functions' (and you determine commands are never called
> recursively or within other commands).

A lot of C functions are like this, in fact, C is like this more or less
in general.  There is no high-level protection from overrunning a buffer,
variables are not automatically initiallized, etc.  The reason for this
is because C is designed to be more of an efficient language than a safe
one.  The programmer is expected to keep his source safe and clean, and
the tradeoff is that the programmer gets a much more efficiently running
program becuase his program is not bogged down with extra checks and what
have you that other languages do behind your back to ensure that you
don't do anything stupid (like try to use strtok() recursively).
strtok() is one such example of a function that is very efficient but not
safe to use in ceartain ways, use it correctly and you're fine.  If you
want a safe lauguage then go use BASIC or Perl or something.  If you're
gonna code in C then realize that a lot of it isn't implicitly safe and
you have to code right to make up for it.

<snip custom strtok() function which is only needed in a few rare
instances when you actually want to use strtok() recursively>.

And now for a (rather rediculous) example of how you _can_ use strtok()
recursively...

void outer_function(void)
{
  char buf[100], *p1, *p2;

  strcpy(buf, "Th-is is a t-est.");

  for (p1 = strtok(buf, " "), p2 = strtok(NULL, "");
       p1; p1 = strtok(p2, " "), p2 = strtok(NULL, "")) {
    printf("%s "strtok(p1, "-"));
 }
}

Here is the output this little function would give...

Th is a t

Regards, Peter

--
   +---------------------------------------------------------------+
   | 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