|
-
March 7th, 2011, 10:03 AM
#16
Re: Do functions compute before being called?
One more question So you guys are saying that the order of evaluation in the statements I have written is undefined. E.g. D_Drmmr said,
"No, the order in which output appears on the screen is the same as the order in which std::cout is called. The problem with your original program is that there are two calls to std::cout (one in sd and one in main), who's order is unspecified."
But yet - the output of this program is ALWAYS A:
"Input array element 0: 5
Input array element 1: 7
Input array element 2: 9
7
The standard deviation of these numbers is 1.63299."
And never, for example, B:
"Input array element 0: 5
Input array element 1: 7
Input array element 2: 9
The standard deviation of these numbers is 7
1.63299."
If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.
I am still unsure of when/where exactly my function is executed.
Thanks again
-
March 7th, 2011, 10:22 AM
#17
Re: Do functions compute before being called?
 Originally Posted by KingGhidorah
One more question  So you guys are saying that the order of evaluation in the statements I have written is undefined. E.g. D_Drmmr said,
It's not undefined, it's unspecified. That basically means the compiler is free to choose any order. Of course, the compiler won't choose something at random, so it will always produce the same evaluation order (at least, with the same configuration).
 Originally Posted by KingGhidorah
If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.
How do you know? Even "undefined" doesn't mean random.
 Originally Posted by KingGhidorah
I am still unsure of when/where exactly my function is executed.
Exactly. 
That's why you should change your code such that you can be sure.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
March 7th, 2011, 11:35 AM
#18
Re: Do functions compute before being called?
 Originally Posted by KingGhidorah
If it were undefined surely the 7 would NOT ALWAYS be before the final sentence? What am I missing here? It seems to me that the order of evaluation is somehow defined.
The issue is not what your current compiler will do, the issue is what any compiler may do with your code. If you took your code, and compiled it on a different compiler, you cannot guarantee the order will be the same.
I am still unsure of when/where exactly my function is executed.
That's the point -- you are not the only one unsure -- even the creator of the C++ language, Bjarne Stroustrup, is unsure.
The order of processing could be all jumbled up for some reason (the first parameter is processed first, then the fifth parameter is processed second, then the second parameter is processed third, etc.). The only thing guaranteed is at the other end when that function you're calling starts, it will have the correct values.
Regards,
Paul McKenzie
-
March 8th, 2011, 04:32 AM
#19
Re: Do functions compute before being called?
A really simple demonstration would be this.
Code:
int x = 0;
F(++x, ++x, ++x);
Parameter parsing is usually either left to right or right to left.
So F is likely to be called with parameters 1, 2, 3 or 3, 2, 1
It's possible it could also be called with any other permutation.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 8th, 2011, 04:49 AM
#20
Re: Do functions compute before being called?
 Originally Posted by JohnW@Wessex
A really simple demonstration would be this.
Code:
int x = 0;
F(++x, ++x, ++x);
Parameter parsing is usually either left to right or right to left.
So F is likely to be called with parameters 1, 2, 3 or 3, 2, 1
It's possible it could also be called with any other permutation.
Actually, its even worst than that, because argument evaluation and argument passing do not have to be done at the same time. In this example, the return type of ++x is a reference. So even if the result of the first ++x is 1, the evalutation of the second parameter changes the result of the first argument, after its evaluation, but before its passed, so you could even end up calling f(3, 3, 3);
This, on my system:
Code:
#include <iostream>
void f(int i, int j, int k)
{
std::cout << i << j << k << std::endl;
}
int main()
{
int x1 = 0;
int x2 = 0;
f(++x1, ++x1, ++x1);
f(x2++, x2++, x2++);
}
prints
Last edited by monarch_dodra; March 8th, 2011 at 04:55 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
March 8th, 2011, 05:20 AM
#21
Re: Do functions compute before being called?
 Originally Posted by JohnW@Wessex
It's possible it could also be called with any other permutation.
 Originally Posted by monarch_dodra
Actually, its even worst than that, because argument evaluation and argument passing do not have to be done at the same time.
Actually, its even worst than that , because the evaluation of such a code gives UB ( in that you can rearrange the evaluation sequence in such a way to read and modify the same scalar variable twice between two sequence points )
for example the code
Code:
#include <iostream>
void f(int i, int j, int k)
{
std::cout << i << j << k << std::endl;
}
int main()
{
int x1 = 0;
int x2 = 0;
f(++x1 + x2++, ++x1 + x2++, ++x1 + x2++);
}
outputs "321" on my system; as you can see, there's no rearrangement of the "basic" operations that reproduces the output, therefore UB has been invoked in this case.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|