Bit of information: maybe useful...

1 view
Skip to first unread message

varun bhardwaj

unread,
Sep 29, 2010, 4:15:51 AM9/29/10
to rsi-c...@googlegroups.com
For CBSE C++ students only:

A classical question in C++ papers:

What is the result of the following statements :
x = 5, y = 11;
x = x ++ + y++;

The answer is that it is undefined.
See http://www2.research.att.com/~bs/bs_faq2.html#evaluation-order (Stroustrup is the guy who made C++).

Compile with the -Wall flag to check whether it's undefined..

To quote a tutorial :
"Yes, it is undefined even if you got an output. It is still undefined even if you get the same answer on your system, your father's system, your grandfather's system and your girlfriend's system. It doesn't make sense to explain some particular output you got. The reason is that if you read a variable twice in an expression where you are also writing it, then it's undefined... "

Stroustrup's answer exactly this :

 " What's the value of i++ + i++?
It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

    v[i] = i++;

Related example:

    f(v[i],i++);


Stroustrup: C++ Style and Technique FAQ
Here, the result is undefined because the order of evaluation of function arguments are undefined.

Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools... "


Hope this was useful..

Varun.
Reply all
Reply to author
Forward
0 new messages