私はこの問題を非常に大きなアプリケーションで発見し、そこから SSCCE を作成しました。-O2
コードに未定義の動作があるか、それとも壊れているかはわかりません。
それをコンパイルすると、 5gcc a.c -o a.exe -O2 -Wall -Wextra -Werror
が出力されます。
ただし、2 つのコメント行のうちの 1 行のコメントを外したり(例: )、コメントを外したり (インライン化を防止)せずにコンパイルすると、 25が出力されます。-O2
-O1
#include <stdio.h>
#include <stdlib.h>
// __attribute__((noinline))
int f(int* todos, int input) {
int* cur = todos-1; // fixes the ++ at the beginning of the loop
int result = input;
while(1) {
cur++;
int ch = *cur;
// printf("(%i)\n", ch);
switch(ch) {
case 0:;
goto end;
case 1:;
result = result*result;
break;
}
}
end:
return result;
}
int main() {
int todos[] = { 1, 0}; // 1:square, 0:end
int input = 5;
int result = f(todos, input);
printf("=%i\n", result);
printf("end\n");
return 0;
}
GCC のオプションは-O2
この小さなプログラムを壊していますか、それともどこかで未定義の動作をしていますか?