Re: fork() Problem?

Richard B. Johnson (root@chaos.analogic.com)
Wed, 5 May 1999 17:53:37 -0400 (EDT)


On Wed, 5 May 1999, Steve VanDevender wrote:

> Richard B. Johnson writes:
> > > To quote the C Reference Manual (which is also the ANSI C
> > > Standard document) in _The C Programming Language, Second
> > > Edition_ by Kernighan and Ritchie:
> > >
> > > A7.17 Assignment Expressions
> > >
> > > There are several assignment operators; all group right-to-left.
> > >
> > > assignment-expression:
> > > conditional-expression
> > > unary-expression assignment-operator assignment-expression
> > >
> > > assignment-operator: one of
> > > = *= /= %= += -= <<= >>= &= ^= |=
> > >
> > > All require an lvalue as left operand, and the lvalue must be
> > > modifiable: it must not be an array, and must not have an
> > > incomplete type, or be a function. Also, its type must not be
> > > qualified with const; if it is a structure or union, it must not
> > > have any member or recursivly, submember qualified with const.
> > >
> > > The type of an assignment expression is that of its left
> > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > > operand, and the value is the value stored in the left operand
> > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > > after the assignment has taken place.
> > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > >
> > > So an assignment expression does have a value, and consequently
> > > can be legally used as a conditional expression in if, while, do,
> > > or for statements.
> > >
> > > In the future, Richard, please trouble yourself to do some real
> > > research rather than treating your personal opinions as
> > > incontrovertible facts.
> >
> > No damnit! Look at the rules. Don't modify them to suit your
> > opinions.
>
> I'm reading the ANSI C standard. The language in the standard is
> quite clear. This is not my personal opinion; it is my
> experience with how C actually works.
>

Again, after the assignment was made, there was no check of the
value. This is what Lint picked up. When you read the standard,
it talks about the value of the left operand. It also talks about
its type because of the promotion rules, that's all your cited
text shows.

Here we have a compound statement in which an assignment is
being made, i.e.,

left = right;

Then we have a conditional expression which tests the result of
an operation:

if(left = right)

As stated, and as Lint picked up, we are now testing if the assignment
occurred, not the value of the left-most operand. Therefore, we have
to make the assignment first:

(left = right) ... Parenthesis for priority.

then test the result:

if ( (left = right) ) ... exactly as Lint has shown.

Lint shows that even this is an error because the result is
not boolean. It really should be.

if ( (left = right) == 0 )

The result of '==' is boolean which 'if' tests. It is, however,
common usage to do:

if(value)

because there usually is a functional equivalent.

Not only does Lint pick up this common error, but so does gcc if
you turn on all the error reporting with -Wall.

xxx.c: In function `main':
xxx.c:13: warning: suggest parentheses around assignment used as truth value

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/