This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: evaluation order of expressions in gcc


Hi,

Addition with numbers is commutative:

a + b = b + a

Therefore, the order does not matter if your two functions f() and g()
return numbers.  If you have + overloaded(*) to operate on objects which are
not commutative under addition (e.g. strings) then I would avoid this type
of construct, and build two operations instead of one to guarantee order.

Something like this:

x += f();
x += g();

(*) I know that's possible in C++, not sure if C does that.

HTH,

David Carter-Hitchin,
GSD-Risk-IT, UBS Investment Bank

On Mon, 29 Mar 2004 laura_rand@women-at-work.org wrote:

> hi, all
>
> i could not find an answer to this question on the net so i'm posting it
> here.
>
> as far as i know, the Ansi C standard does not specify the order of
> evaluation of expression arguments. so if i wrote:
>
> x = f() + g()
>
> it would be up to the compiler whether f() or g() would be called first.
>
> my question is, does anybody know how does the current version of gcc choose
> whether to call f() or
> g() first? on which optimization options does this depend, if any?
>
> after making a few experiments, it looks like gcc usually does left-to-right
> evaluation (first calls f() and then g()). so i would like to know in which
> cases (if any) this would not be true. or if this information is not known
> for the current version, then for any previous version would also be
> appreciated.
>
> to clarify: i am not interested in the evaluation order of arguments to a
> function call, or left vs right side of an assigment. also i am not interested
> in expressions involving pre- or post- increment or decrement or anything
> such. i understand that these may be a problem. so my question is only regarding
> function calls in expressions with normal arithmetic (also pointer
> arithmetic) and compare operators.
>
> laura
>
> --
> +++ NEU bei GMX und erstmalig in Deutschland: TÜV-geprüfter Virenschutz +++
> 100% Virenerkennung nach Wildlist. Infos: http://www.gmx.net/virenschutz
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]