Re: Is the behavior unspecified or can it the expression be evaluated?
coolguyaroundyou@gmail.com wrote:
Here, analyse this snippet :
{
int a=1,b=0,c=0;
b=a+ ++a;...........Statement 1
a=1;
c=a+ a++;...........Statement 2
}
Both the statements,1 and 2, have side-effects. The standard says that
the order of evaluation of side-effects is unspecified. So,will the
values stored in b and c be implementation-defined?
No, they will be undefined. The Standard says that the object's value
will be accessed only to determine the value to be stored between the
sequence points (see 5/4). Since you're trying to access 'a' twice,
once to determine the future value of 'a' and the second time to
calculate the value of the left-hand side of the assignment expression,
you're in violation of the Standard. The behaviour is undefined.
I tried this in 2 compilers and the result was same, but the result of
the following in the same 2 compilers was different:
{
int a=1;
std::cout<<a<<++a;...........Statement 1
a=1;
std::cout<<a<<a++;...........Statement 2
}
Any reasons for such behavior in this case and also for the behavior
in the former code??
Here the behaviour is unspecified because it depends on the order of
evaluation of the function arguments, which itself is unspecified. It
is not undefined, though.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask