arnold said:
I allways thought that a statement like i++ meant
first use the value of i then increment it
No, that's not it. It means "increment i" and also "the result of the
expression is the value that i had before being incremented". This
implies nothing at all about the order in which the increment or the use
of the value of the expression occur. It's entirely possible that the
compiler will generate code to store off the value of i, increment i
immediately, and then use that temporary value in another expression.
Or alternatively, generate code to increment i first, then make a copy
and subtract one from the copy. Or, alternatively, generate a machine
code instruction that performs the increment simultaneously with using
the original value in another expression.
More importantly, your macro expands to increment i twice, not just
once. The variable i is therefore modified twice, in an undefined order
and perhaps even in parallel, and the result is therefore completely
undefined.
since this is a macro does it change how it works or did i fundamentally
misunderstand.
The macro just expanded PRODUCT(i++) to "i++ * i++", which I assume you
knew, since you wrote the expanded form in the subject line. It's the
multiple modifications to i between sequence points that yield the
undefined behavior.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation