if else Vs ternary

snipped-for-privacy@kapsi.spam.st>> Sim>>> Several pointer variables were defined within main(). Each

As another poster said, be careful whether you are talking about a volatile pointer, or a pointer to volatile (that's what you want here).

With debugging, it often helps to add a few extra volatiles so that you can track them better in the debugger. Another trick is to make a local or file-scope static into a global variable. Also consider adding "noinline" attributes to functions you are particularly interested in.

And when you generate debugging information, gcc can provide different levels of debug symbolic information. With the simpler levels, gdb can't keep track of variables that move around from register to register

- you'll get the wrong information. With the higher levels, it can track variables through multiple registers, or multiple variables in the same register.

Reply to
David Brown
Loading thread data ...

This would be the result of the "-fif-conversion" optimisation flag which is designed to do exactly that sort of re-arrangement. It is enabled automatically by -O1 or above, because it is so effective with processors like this.

Reply to
David Brown

Because it's different code that is dealt with differently. "if" is a statement whereas ?: is an expression. The two movs in the ternary expression relate to different sub-expressions. The first stores the value of the ?: subexpression ready for use in the wider expression, which just so happens to be an assignment which is what the second mov represents.

You may ask why not do it in one but the templates for implementing an assignment and implementing a conditional expression are presumably separate. It is up to the optimiser to eliminate any consequent redundancies - the very optimiser you have turned off. Therefore you are actually asking why the compiler did what you told it.

The if statement is different precisely because it _is_ a statement

- there is no value to consider. The decision making mechanism is separated from the evaluation of the expression chosen. That expression is a simple assignment with no intermediate results and therefore nothing that needs to be stored along the way.

--
Andrew Smallshaw
andrews@sdf.lonestar.org
Reply to
Andrew Smallshaw

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.