Syncnister <harishashim@gmail.com> wrote in news:b6e82241-d3e3-4764-8384-
5feea0bad5ad@s13g2000prd.googlegroups.com:
[...]
> bool MyFunction()
> {
> if (sometest)
> {
> return false;
> }
>
> somecode;
> somecode;
>
> // missing return
> }
In the (paranoid?) fear of forcing any non-zero cost on the running code
the C and C++ languages allow to omit the return statement in code
branches which are never executed. The compilers thus only give warnings
for such code. If the branch is actually taken, you get undefined
behavior, i.e. anything can happen.
In this case, on Intel processors, what *probably* *might* happen is that
the register used for return value is not touched and after the function
return it contains some random value. The value is interpreted as a bool
by the calling function. The interpretation *probably* recognizes zero as
false and anything non-zero as true. Thus chances to get apparent
response 'true' *might be* greater, which might have misled the original
programmer to think that his code is correct.
The only way to fix the code is to add proper return statements (and
compile the code with all warnings enabled!). If the branch is never
executed, you don't lose anything; if the branch is executed you avoid
UB.
hth
Paavo