Re: Function call evaluation order



Frederico Pissarra wrote:
I agree that the code used as example is a bit confusing - after all,
i is incremented after each function call or after all funcions?. But
this is not "undefined" behavior!
In every compiler I tested this code (GCC, Borland C++, Visual C++ 6,
7 and 8 and Watcom C++) the result is the same... Because structure
member operator, function calls and array offset operator have higher
precedence than pos or pre increment, those are made AFTER the entire
sentence... this is the expected behavior in C language!

Not so. You're guaranteed that all of the evaluation of the parameters of a
function call complete before the function call begins, but when you have a
"full expression" (everything up to the semicolon) that contains multiple
function calls, there are no guarantees at all about the order of evaluation
of the parameters of a given function nor the order in which functions will
be called (except as necessary to obey precedence rules).

Are you sure you tried this with VC8? The following program:

#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
char* data[] = { "one ", "two ", "three " };
int i = 0;
string s;
s.assign(data[i++]).append(data[i++]).append(data[i++]);
cout << s << endl;

i = 0;
s.assign(data[i++]);
s.append(data[i++]);
s.append(data[i++]);
cout << s << endl;
}


compiled with VC8 with out without optimization produces

one one one
one two three

as it's output. If precedence alone dictated behavior, the two lines of
output would be the same. That is undefined behavior.

-cd


.



Relevant Pages

  • Re: Need Explanation on Lvalue Assignment for the following snippet
    ... int main ... the old value of the pointer is dereferenced, yielding an object - ie an ... access the value and then assign them and then increment the pointers ... precedence, but *not* to do with order of evaluation. ...
    (comp.lang.c)
  • RE: Robby, __int8 limited to: -128 to 127.
    ... And yes an 8bit int cannot hold a value of 256. ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... In order to access the data from my external memory I need a three byte ... and increment the A0 value from 1 to 2. ...
    (microsoft.public.vc.language)
  • Writing Classes
    ... Clock application ... private int hr; //store hours ... public void setTime{ ... //Method to increment the time by one second ...
    (comp.lang.java.programmer)
  • Re: man 3 switch
    ... the switch statement in gcc may not be exactly the same as the switch ... As C is an ISO standard, I sincerely doubt there would be any ... of arithmetic operators of equal precedence in the same statement (in ... increment the value of a, ...
    (Fedora)
  • Re: Definition of expression and statement.
    ... The fact that a while loop is recognized ... behavior, e.g., if a is an "int" variable and is initially set to ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)

Loading