[comp.lang.c] side effects in argument lists

mikpe@mina.liu.se (Mikael Pettersson) (11/28/88)

Consider the following program:
---snip---
#include <stdio.h>
int count = 0;
void bletch(int junk) {
	printf("count == %d\n", count); /* 0 or 1 ? */
}
main() {
	bletch(count++);
	exit(0);
}
---snip---
This program exemplifies the situation where the behaviour of the
callee depends on whether the caller has completed *all* of the
side effects from evaluating the arguments of the call or not.

Q: what sayeth the dpANS about this?
If count++ _is_defined_as_ (++count, count-1) then I guess things
are cool, but what if the definition is less strict?


/Mike
-- 
Mikael Pettersson           ! Internet:mpe@ida.liu.se
Dept of Comp & Info Science ! UUCP:    mpe@liuida.uucp  -or-
University of Linkoping     !          {mcvax,munnari,uunet}!enea!liuida!mpe
Sweden                      ! ARPA:    mpe%ida.liu.se@uunet.uu.net

chris@mimsy.UUCP (Chris Torek) (11/30/88)

In article <1077@mina.liu.se> mikpe@mina.liu.se (Mikael Pettersson) writes:
>Consider the following program:

[edited]
>void bletch(int junk) { printf("count == %d\n", count); /* 0 or 1 ? */ }
>main() { bletch(count++);

>This program exemplifies the situation where the behaviour of the
>callee depends on whether the caller has completed *all* of the
>side effects from evaluating the arguments of the call or not.

>Q: what sayeth the dpANS about this?

The dpANS sayeth that the program is to print `1'.  All side effects
are to have completed by the next `sequence point'.  The list of
sequence points includes comma expressions, `&&', `||', and not
least, function calls.  Obviously, statement bounardies (semicolons)
are sequence points as well.

>If count++ _is_defined_as_ (++count, count-1) then I guess things
>are cool, but what if the definition is less strict?

The definition is less strict, but the side effect is done by the
next sequence point.  Insert a sequence point and your side effect
is guaranteed; without it, it is not.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

evil@arcturus.UUCP (Wade Guthrie) (12/03/88)

In article <14758@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes:
> . . . All side effects
> are to have completed by the next `sequence point'.  The list of
> sequence points includes comma expressions, `&&', `||', and not
> least, function calls.  Obviously, statement bounardies (semicolons)
> are sequence points as well.

I believe that binary operators such as '-' are also `sequence points'.
Doesn't this cause the behavior of:

	a = b++ - b++;

to be undefined (different answers depending on right-to-left evaluation
versus left-to-right)?


Wade Guthrie
Rockwell International
Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)

chris@mimsy.UUCP (Chris Torek) (12/04/88)

In article <2916@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes:
>I believe that binary operators such as '-' are also [dpANS] `sequence points'.

No, none of +, -, *, /, or % introduce a sequence point.

>Doesn't this cause the behavior of:
>
>	a = b++ - b++;
>
>to be undefined (different answers depending on right-to-left evaluation
>versus left-to-right)?

You must be confused about sequence points.  The order of evaluation of
objects on the left and right sides of the arithmetic operators listed
above is undefined.  The *only* purpose for a sequence point is to
guarantee side effects, so a sequence point within an operation whose
evaluation order is undefined would be, er, `pointless'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris