122 DECEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
recently read an interesting article [C++ Report,
Vol.
6, no.
3, ‘How to write buggy programs’ by
Andrew Koenig], which is about writing incorrect
programs.
I’ve taken the following...
More
122 DECEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
recently read an interesting article [C++ Report,
Vol.
6, no.
3, ‘How to write buggy programs’ by
Andrew Koenig], which is about writing incorrect
programs.
I’ve taken the following (slightly
modified) piece of code from that article to
illustrate how compiler optimisers work:
extern void foo();
int main() {
if(0) foo();
}
The foo function is just declared and is not defined in the
program.
The if condition always evaluates to ‘false’.
Should
this program link fine or result in a linker error complaining
that the definition of foo is not found? That will depend on
the compiler.
Some smart compilers see that the statement
for if(0) will never get executed, and will therefore not
generate a call to foo at all—so the linker won’t complain.
However, not all compilers do such smart work in default
compilation mode and hence we might get a linker error.
(Such compilers might detect the ‘unreachable function call’
at high
Less