[net.lang.c] op= construction

cgw@mruxe.UUCP (C Waldman) (06/09/86)

	Quick question-

	In a loop, an array element is incremented, then the array index
	is advanced to the next element. I recently coded this with
	
	....
	total[i++] += f;
	....
	
	After a couple of "Memory fault- core dumped" messages, I realized
	that i is getting incremented twice. I broke it into two lines and
	everything was ok, but I'm wondering why this construction doesn't
	work.

ark@alice.UucP (Andrew Koenig) (06/10/86)

>	Quick question-

>	In a loop, an array element is incremented, then the array index
>	is advanced to the next element. I recently coded this with
	
>	....
>	total[i++] += f;
>	....
	
>	After a couple of "Memory fault- core dumped" messages, I realized
>	that i is getting incremented twice. I broke it into two lines and
>	everything was ok, but I'm wondering why this construction doesn't
>	work.

Easy -- your compiler is broken.

	total[i++] += f;

should be equivalent to

	total[i] += f;
	i++;

If it isn't, your compiler is busted.  Simple as that.

cgw@mruxe.UUCP (C Waldman) (06/10/86)

Thanks for your quick reply.  The compiler in question is cc running on our
V7 Release 2  Unix machine, and I'm amazed that I found a bug in it!  I reported
to root, awaiting response.

dant@tekla.UUCP (Dan Tilque) (06/11/86)

>From: cgw@mruxe.UUCP (C Waldman)
>	Quick question-
>
>	In a loop, an array element is incremented, then the array index
>	is advanced to the next element. I recently coded this with
>	
>	....
>	total[i++] += f;
>	....
>	
>	After a couple of "Memory fault- core dumped" messages, I realized
>	that i is getting incremented twice. I broke it into two lines and
>	everything was ok, but I'm wondering why this construction doesn't
>	work.


Your compiler seems to have a bug in the way it evaluates total[i++].

The gospel according to K&R (p. 191):

	       The behavior of an expression of the form E1 op= E2 may be
	inferred by taking it as equivalent to E1 = E1 op (E2); however,
	E1 is evaluated only once.

Your compiler seems to be evaluating total[i++] twice, once for the left
side of the =, and once for the right.  I ran a simple test on our compiler
(cc) and it seems to work right.

==========================================================================
Dan Tilque			Googol in, Googolplex out.
(tektronix!dadla!dant)
==========================================================================

gwyn@brl-smoke.ARPA (Doug Gwyn ) (06/15/86)

In article <108@mruxe.UUCP> cgw@mruxe.UUCP (C Waldman) writes:
-	In a loop, an array element is incremented, then the array index
-	is advanced to the next element. I recently coded this with
-	....
-	total[i++] += f;
-	....
-	After a couple of "Memory fault- core dumped" messages, I realized
-	that i is getting incremented twice. I broke it into two lines and
-	everything was ok, but I'm wondering why this construction doesn't
-	work.

Either you were incrementing i somewhere else too (e.g. in the for() head),
or else your compiler has a bug.  The whole point of the op= operators is
that the left side is supposed to be evaluated only once.

davidsen@steinmetz.UUCP (Davidsen) (06/17/86)

In article <108@mruxe.UUCP> cgw@mruxe.UUCP (C Waldman) writes:
>	Quick question-
>	....
>	total[i++] += f;
>	....
>	
>	After a couple of "Memory fault- core dumped" messages, I realized
>	that i is getting incremented twice. I broke it into two lines and
>	everything was ok, but I'm wondering why this construction doesn't
>	work.

I tried it on a VAX, a Sun, and XENIX, looking at the code with the -S
option. They all work as desired. Therefore I believe that either your
program has another problem or (most likely) your compiler is broken.
Please let me know (or post) what you find, and which compiler/machine
you are using.
-- 
	-bill davidsen

  ihnp4!seismo!rochester!steinmetz!--\
                                       \
                    unirot ------------->---> crdos1!davidsen
                          chinet ------/
         sixhub ---------------------/        (davidsen@ge-crd.ARPA)

"Stupidity, like virtue, is its own reward"