|
From: | Ivan Vučica |
Subject: | Re: ProjectCenter Editor parenthesis highlighting segfault |
Date: | Wed, 27 Aug 2014 18:36:50 +0100 |
That looks like a serious bug in your compiler then. Your reasoning is right, the body should not be entered with any value of i greater than 1 and if it does the compiler is generating incorrect code.
> I expected that i could never get greater than 1 in the body of the loop as the boolean _expression_
> must return false when i reaches 2. I ran this several times and i always reaches 3 bevor the segfault
> occurs.
Consider the following function:
int table[4];
bool exists_in_table(int v)
{
for (int i = 0; i <= 4; i++) {
if (table[i] == v) return true;
}
return false;
}
What does this have to do with time travel, you ask? Hang on, impatient one.
First of all, you might notice the off-by-one error in the loop control. The result is that the function reads one past the end of the table array before giving up. A classical compiler wouldn't particularly care. It would just generate the code to read the out-of-bounds array element (despite the fact that doing so is a violation of the language rules), and it would return true if the memory one past the end of the array happened to match.
A post-classical compiler, on the other hand, might perform the following analysis:
- The first four times through the loop, the function might return true.
- When i is 4, the code performs undefined behavior. Since undefined behavior lets me do anything I want, I can totally ignore that case and proceed on the assumption that i is never 4. (If the assumption is violated, then something unpredictable happens, but that's okay, because undefined behavior grants me permission to be unpredictable.)
- The case where i is 5 never occurs, because in order to get there, I first have to get through the case where i is 4, which I have already assumed cannot happen.
- Therefore, all legal code paths return true.
As a result, a post-classical compiler can optimize the function to
bool exists_in_table(int v)
{
return true;
}
[Prev in Thread] | Current Thread | [Next in Thread] |