Re: if (blah) ...

From: Daniel A. Koepke (dkoepke@california.com)
Date: 08/11/99


On Aug 12, Chris Proctor spake to the list:

> Occasionally it will do "this" instead of "something else" even if "blah"
> is negative or null.

It only does "this" if "blah" is true (i.e., it's not zero/NULL).  A
negative number is non-zero, and thus counts.  "if (blah)" is not the
proper way to check for positive integers, only for things that are not
NULL.  Note that free() DOES not set memory back to NULL, thus:

    free(blah);
    if (blah) printf("This is still printed.\n");

Will print, "This is still printed.," but the memory is NOT valid for use.

> or if (blah > 0), depending on what type of variable blah is.

This probably isn't a good idea.  In a lot of places, "if (blah)" is meant
NOT "if (blah > 0)".

> Anyone know why this would suddenly have started happening? Is it to
> do with versions of compilers, operating systems, or something else
> entirely?

This behavior has been the norm for as long as C has been around.

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST