This is an example to illustrate my question which involves some much more complicated code that I can't post here.
#include <stdio.h>
int main()
{
int a = 0;
for (int i = 0; i < 3; i++)
{
printf("Hello
");
a = a + 1000000000;
}
}
This program contains undefined behavior on my platform because a
will overflow on the 3rd loop.
Does that make the whole program have undefined behavior, or only after the overflow actually happens? Could the compiler potentially work out that a
will overflow so it can declare the whole loop undefined and not bother to run the printfs even though they all happen before the overflow?
(Tagged C and C++ even though are different because I'd be interested in answers for both languages if they are different.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…