473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

order of evaluation

//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

Jul 23 '05 #1
3 1158

"ASobol" <so***@mccme.ru> wrote in message
news:11**********************@o13g2000cwo.googlegr 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 <su*********@gmail.com>
Jul 23 '05 #2

Sumit Rajan wrote:
"ASobol" <so***@mccme.ru> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #3
"msalters" <Mi*************@logicacmg.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Undefined behaviour.


Wrong, unspecified.


You're right. Thanks for pointing that out.

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.