Почему мой код C++ выдает ошибку при использовании -O2?C++

Программы на C++. Форум разработчиков
Гость
Почему мой код C++ выдает ошибку при использовании -O2?

Сообщение Гость »


Here is my code.

Код: Выделить всё

#include  struct Node {     int x; }; Node f(int A, int B) {     if (A < 0) return {0};     return {A / B + (A % B != 0)}; } Node g(int C, int D) {     if (C > 0) return {0};     return {C / D + (C % D != 0)}; } int main() {     int n;     scanf("%d", &n);     f(1, 1);     Node t = g(-1, n);     printf("%d\n", t.x); } 
Then I use:

Код: Выделить всё

g++-13 tmp.cpp -o tmp -O0
. I input , it should output .
But

Код: Выделить всё

g++-13 tmp.cpp -o tmp -O2
I get .

I think there is no undefined behavior. Why I get an wrong result?

OS: Linux x86-64 ubuntu23.10 6.5.0-21-generic
gcc version: >=9.2.0


Источник: https://stackoverflow.com/questions/781 ... n-i-use-o2

Вернуться в «C++»