Imaginary losses
Posted Mar 27, 2008 7:56 UTC (Thu) by
njs (guest, #40338)
In reply to:
Imaginary losses by alankila
Parent article:
Striking gold in binutils
malloc isn't the example to think of here, because yeah, usually you just abort. And the problem isn't that first if statement, where you detect the error in the first place. The problem is that in well-written C code, practically *every* function call has some sort of error checking wrapped around it, because errors in that function need to detected and propagated back on up the stack. It's the propagating that really hurts, because you have to do it with if statements, and if statements are expensive.
Compare C:
error_t foo() {
char * blah;
error_t e = bar(blah);
if (!e)
return e;
e = baz();
if (!e) {
free(blah);
return e;
}
/* ... */
}
versus C++:
void foo() {
std::string blah = bar();
baz();
...
}
One might think that the C++ code has "hidden" if statements; for old C++ compilers, that was true. Modern compilers, though, use Extreme Cleverness to avoid that sort of thing. (If you're curious for details, just g++ -S some simple programs and see.)
(
Log in to post comments)