473,587 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Observable behavior and instruction reordering

<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)< <endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)< <endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?

Any insights as to what I am not grokking would be appreciated.
Jul 22 '05 #1
4 2647

"DaKoadMunk y" <da*********@ao l.com> wrote in message
news:20******** *************** ****@mb-m22.aol.com...
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)< <endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)< <endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data and calls to library I/O functions how does it know that it is not changing the meaning of a program?

Any insights as to what I am not grokking would be appreciated.


If the instructions were reordered in the way you suggest then you would not
see cout << 12 followed by cout << 10 (you would see cout << 24 followed by
cout << 12), therefore the observable behaviour would have changed because
the values of the arguments used in a standard library call would have
changed, and therefore the compiler would not be allowed to do that
reordering.

John
Jul 22 '05 #2
da*********@aol .com (DaKoadMunky) wrote in
news:20******** *************** ****@mb-m22.aol.com:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)< <endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)< <endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction
reordering as it relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in
function Foo could be swapped. That of course would change the
meaning of Foo. However, the following quote from the Draft Ansi C++
Standard makes me think that the meaning of Foo is not observable
behavior...

"The observable behavior of the abstract machine is its sequence of
reads and writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1
will execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to
volatile data and calls to library I/O functions how does it know that
it is not changing the meaning of a program?


Because the people who created it, told it to only reorder those things
that won't break the meaning of the program. Observable behaviour is not
the only criteria when dealing with instruction reordering, obviously.

Cheers.
--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."

Jul 22 '05 #3
On 09 Aug 2004 11:17:04 GMT, da*********@aol .com (DaKoadMunky) wrote:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)< <endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)< <endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?
Yes, any standards conforming compiler should produce a program that
outputs:
12
10
My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.
"calls to library I/O functions" includes what is passed to those
functions! i.e. any reorderings that the compiler does must not change
the appearance of the output of a conforming program in any way.
When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?


If it can't determine that it won't make a difference to observable
behaviour, it won't reorder (since it is forbidden by the standard).

Tom
Jul 22 '05 #4
DaKoadMunky wrote:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)< <endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)< <endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?

Any insights as to what I am not grokking would be appreciated.


A translator is allowed to set the ordering of the instructions as long
as the functionality remains the same.

In your example, changing the order of the mathematical assignments
will change the meaning. However, there is nothing stopping a
translator changing the order of the assignment operation.

Your function evaluates the expression: f(x,y) = x * y + y;
The actually assignment of the value to result could be postponed
or not even occur at all (since the value is returned and not
stored in any static memory location). For example, your function
could be translated as:
int Foo(int x, int y)
{
return x * y + y;
}
The above function provides the same functionality, but has
no assignment operations.

There are no libraries calls, no I/O and no volatile or static
variables.

However, changing the order of the operations will change the
functionality.

The rules allow for compilers to optimize code. The qualifiers
"static", "volatile" and "mutable" are used to control the
optimization. During debugging, I may declare a variable
in a function as "static" so that the variable isn't "optimized
away". I use "volatile" when accessing hardware values so that
the compiler doesn't "optimize away" a variable that is read
only once.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

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

Similar topics

10
4253
by: tgh003 | last post by:
So lets assume I have a list of tasks in db table (table looks like: ID, task, sort) And I want to reorder the task list without updating all the records in the table. I dont want to run a sql update stmt against every record to update the "sort" field. Any other easy way to do this? there has got to be a simple algo out there.
3
2308
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click on it and choose Open from the context menu, I want the form to reappear. For that, I got the point covered. Even when the form is minimize, the...
5
6869
by: John Dann | last post by:
Can anyone point me to a tutorial on reordering items within a single listbox using the mouse, ie drag and drop, specifically with vb.net. Google shows me various half-references eg to related controls, vb6 or whatever, but nothing that seems to relate specifically to vb.bet and reordering within a single listbox control. Thanks JGD
5
2028
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default...
15
1828
by: loic-dev | last post by:
Hello everybody! Assume that I have the following function: void call_once(int* done, void(*routine)(void)) { if (!(*done)) { /* L1 */ routine(); /* L2 */ *done = 1; /* L3 */
7
1755
by: =?Utf-8?B?QnJpYW4gS3Vueg==?= | last post by:
Hello, I'm new to the boards, and I've been struggling with a problem porting my company's code from Visual C++ 6.0 to Visual C++ 2005. We've found some crashes that I've traced to the 'c_str()' member of the standard string class. I've debugged into the deepest layers of the system code and cannot explain this. What we have is a...
12
3044
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the difference. For example: Consider the following code: a = i; We say that the above expression statement produces undefined
36
2053
by: dspfun | last post by:
Hi! Is there any way in C that one can guarantee that instructions are executed in the same order as they are written in the program? For example, say I have the following: instruction1; instruction2; instruction3;
20
1363
by: Tommy Vercetti | last post by:
Hi - Great group! I have 2 queries about undefined behavior: 1) Is the following code undefined? float myfunction(float f) {
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.