2 ms·
That's shoddy programming - it obviously contains an error. As someone who compiles with "-Wall -Wextra -Werror", I believe that this example program shouldn't
by pdkl95 11y ago
That's shoddy programming - it obviously contains an error.
As someone who compiles with "-Wall -Wextra -Werror", I believe that this example program shouldn't even be allowed to compile, even though the standard allows it.
The variable should really be initialized explicitly, which shouldn't be expensive at runtime.
int i = 1; /* or whatever */
Alternately, if the programmer intended to only print when some_big_complicated_function() return true - which is a strong possibility because they only wrote a value to "i" for that case - then the bug is in the position of the printf and possibly the variable definition, if you're using >=C99.
#include <stdlib.h>
#include <stdio.h>
int some_big_complicated_function(void);
int main() {
if (some_big_complicated_function()) {
int i = 0;
printf("%d\n", i);
}
return EXIT_SUCCESS;
}
> the resulting program would go much, much slower due to all the checks.
That depends on a lot. It can be (very) significant if you're talking about the middle of a hot loop; in the given example that runs once per main(), the difference is negligible.
For better examples and a discussion of why it can be very hard to issue useful warnings when optimizing around undefined behavior, see LLVM's series of articles that should is mandatory reading for anybody working with C/C++.
1/3 - http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
2/3 - http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
3/3 - http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...