Quote:
Originally posted by Arryn:
The problem comes in that the compiler's optimization will substitute the same call to the random number function for both die rolls. It won't make the two rand() calls the coders intend. What it does is make one call and plug the same value into both places. The optimizer does not know that in this circumstance, two calls to the same function do not return the same value.
|
Consider the following:
code:
void print_blank_line { printf("\n"); }
void print_2blank_lines()
{
print_blank_line();
print_blank_line();
}
Do you think compiler/optimizer will call print_blank_line just once? The similar situation will happen in the following:
code:
std::vector<int> x;
void foo()
{
x.push_back(1);
x.push_back(1);
}
Will push_back be called only once (parameters are the same)?
If any compiler does make one call instead of 2 in these cases, you're not likely to build anything usable with it. So it's safe to assume that any common compiler doesn't have this problem.