Re: fork() Problem?

Richard B. Johnson (root@chaos.analogic.com)
Wed, 5 May 1999 15:10:33 -0400 (EDT)


On Wed, 5 May 1999, Steve VanDevender wrote:

> Richard B. Johnson writes:
> > > if (pid = fork())
> > ^^^^________ logical test of an assignment? This will always
> > be true!
>
> No. An assignment expression has the value of the value
> assigned. This allows expressions like a = b = c ('=' is
> right-associative). It will be true if the assigned value is
> true (nonzero), and false if the assigned value is false (zero).
>
> However, it is generally more clear and less error prone to make
> such tests explicit (i.e. (a = b) != 0).
>

No. Definitely not! The gcc compiler 'fixes' very obvious and
awful bugs.

#include <stdio.h>

int foo()
{
return 0;
}

int main()
{
int i;
if(i=foo())
(void)puts("If this is not seen, it is a compiler BUG!");

return 0;
}

LCLint 2.2a --- 04 Sep 96

xxx.c: (in function main)
xxx.c:13,5: Test expression for if is assignment expression: i = foo()
The condition test is an assignment expression. Probably, you mean to use ==
instead of =. If an assignment is intended, add an extra parentheses nesting
(e.g., if ((a = b)) ...) to suppress this message. (-predassign will suppress
message)
xxx.c:13,5: Test expression for if not bool, type int: i = foo()
Test expression type is not boolean or int. (-predboolint will suppress
message)

Finished LCLint checking --- 2 code errors found

I started to notice sloppy code being written this way several years
ago when 'gcc' started to allow it. Just because 'gcc' fixes bad code
doesn't mean it's the way to go (unless it fixes the source at the
same time).

Cheers,
Dick Johnson
***** FILE SYSTEM WAS MODIFIED ***** Penguin : Linux
version 2.2.6 on an i686 machine (400.59 BogoMips). Warning : It's hard
to remain at the trailing edge of technology.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/