So, I was compiling a computer program today, and got this error:
“warning: control reaches end of non-void function”.
And people say philosophy and theology use words oddly to be obscure [grin].
(What this means is that in a function where I say “This function must return a value”, it’s possible to hit the end of the function and not return a value.)
August 4, 2011 at 6:11 pm |
Not exactly right. Control is rather stupid. The following raises the warning when using Wall, even though argc is always >= 1. Then again, the compiler can only check semantics when provided with them.
int main ( int argc, char** argv) { if (argc) return 2; }
N
August 9, 2011 at 9:55 am |
Well, in this case, the compiler is technically right; it is possible, it just won’t ever happen due to how the system passes in argc (it counts the command as well). I seem to recall mention of some systems that actually DON’T count the command, at least in the argv part. So it’s just warning you about a possible problem (and, to be honest, really bad code [grin]).
A case like this, however, is more egregious:
bool foo() {
int a = 5;
if(a == 5) {
return true;
}
}
I haven’t tried it, but I suspect the function would still complain. But if you aren’t a real-time compiler, it’s massively hard to write the compiler to figure this case out, since you need to keep track of contexts and their constant assignments, potentially from a statement that was far back in the code, and in a manner that you’d only need for this specific case.