[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Is the behavior unspecified or can it the expression be evaluated?

coolguyaroundyou

9/24/2008 7:59:00 PM

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?

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??
2 Answers

Victor Bazarov

9/24/2008 8:30:00 PM

0

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

James Kanze

9/25/2008 8:30:00 AM

0

On Sep 24, 10:29 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> coolguyaround...@gmail.com wrote:
> > Here, analyse this snippet :

[...]
> > 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.

Yes it is. There's still no sequence point between the read of
a in the first sub-expression, and the modification in the
second. A function call introduces a sequence point, but
there's nothing here that would require the operator<< function
to be called between those sub-expressions.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34