Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > order of evaluation

Reply
Thread Tools

order of evaluation

 
 
ASobol
Guest
Posts: n/a
 
      02-15-2005
//code.begin()
#include <iostream>

int g (int i)
{
static const int j = i;
return j;
}

int main (int, char**)
{
std::cout << g(0) << g(1) << g (2) << g(3);
return 0;
}
//code.end()

Is output of this program undefined due to C++ standart?
Compilers i have access to (msvc 7.1 and gcc 3.3) both produce "3333"
output.


--Sasha

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      02-15-2005

"ASobol" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> //code.begin()
> #include <iostream>
>
> int g (int i)
> {
> static const int j = i;
> return j;
> }
>
> int main (int, char**)
> {
> std::cout << g(0) << g(1) << g (2) << g(3);


The compiler is allowed to determine the order in which the 4 calls to g()
are executed.

> return 0;
> }


> //code.end()
>
> Is output of this program undefined due to C++ standart?


Undefined behaviour. See:
http://www.parashift.com/c++-faq-lit...html#faq-38.16
http://www.parashift.com/c++-faq-lit...html#faq-38.15
http://www.langer.camelot.de/Article...ncePoints.html



Regards,
Sumit.
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      02-15-2005

Sumit Rajan wrote:
> "ASobol" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > //code.begin()
> > #include <iostream>
> >
> > int g (int i)
> > {
> > static const int j = i;
> > return j;
> > }
> >
> > int main (int, char**)
> > {
> > std::cout << g(0) << g(1) << g (2) << g(3);

>
> The compiler is allowed to determine the order in which the 4 calls

to g()
> are executed.


Yes, but it may not execute them concurrently (unless you cannot detect
it, of course. "as if"-rule ). One call must be made first. It may be a
different choice on every run, though.

> > Is output of this program undefined due to C++ standart?

>
> Undefined behaviour.


Wrong, unspecified. There are proper sequence points between the
calls, the only thing unspecified is the order of sequence points.
No variable is modified twice between sequence points. In fact,
the only variable modified is static const int j, which by
definition cannot be modified twice at all.

Regards,
Michiel Salters

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      02-15-2005
"msalters" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> Undefined behaviour.

>
> Wrong, unspecified.


You're right. Thanks for pointing that out.

Regards,
Sumit.
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
evaluation order Nan Li Java 11 11-15-2005 03:26 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E04 - jamPersist Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 18 04-09-2005 04:45 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 74 04-04-2005 05:29 PM
Evaluation order for a=b Xavier Decoret C++ 1 07-03-2003 07:09 PM



Advertisments