C return a++ - is it safe?

R

Richard Bos

Tor Rustad said:
"Testing can show the presence of errors, but not their absence."
-E. W. Dijkstra

"Beware of bugs in the above code; I have only proved it correct, not
tried it." - Donald E. Knuth
Still, there is a non-zero probability, that the same compiler fault can
hit all of them, e.g. via some common mode of failure.

Highly likely, in fact, if they're all working from the same specs.

Richard
 
O

ozbear

Not quite "any". In a function call, f(a++) the side-effects must
have happened by the time function is entered. Of course, if the "use
it" is "push it" and the function call follows the inc (and another
mov) we are OK, but the same could also be done for the return (the
"use it" being put into register used for return values).
<snip>

I am not quite sure I understand what you are saying. While a
sequence point does occur just before and after the function call, the
value of /a/ passed will be its pre-incremented value.

Oz
 
B

Ben Bacarisse

<snip>

I am not quite sure I understand what you are saying. While a
sequence point does occur just before and after the function call, the
value of /a/ passed will be its pre-incremented value.

Yes. I was making a very small point. The discussion was about how
likely it is that 'return a++;' will be miss-compiled. James Harris
pointed out that "in any context other than return" the compiler can
load the value of a, generate the code that uses it, then emit an
increment instruction. I was just pointing out that 'f(a++);' is
similar -- the code that uses 'a' gets the prior value but the
increment must have happened before any of that code is executed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top