D
DaKoadMunky
<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.
#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.