Absent of return in old C++ codes

S

Syncnister

Hello everyone,

I have found some old c++ code with function defined as returning
boolean. The thing is that doesn't have any return statement at the
end. Looking at how the function is being used, suggesting that it is
expected to return true by default.

bool MyFunction()
{
if (sometest)
{
return false;
}

somecode;
somecode;

// missing return
}

My compiler is the latest GCC / G++ and the target system is HPUX. My
testing environment (where I found the problem) is Cygwin. I suspect
that in HPUX I'm getting return value true as default, but on cygwin
I'm getting false.

I am trying to explain why this happen (to my self) and to justify
adding return false before the function exit. Appreciate any thought.
 
R

red floyd

Syncnister said:
Hello everyone,

I have found some old c++ code with function defined as returning
boolean. The thing is that doesn't have any return statement at the
end. Looking at how the function is being used, suggesting that it is
expected to return true by default.

bool MyFunction()
{
if (sometest)
{
return false;
}

somecode;
somecode;

// missing return
}

My compiler is the latest GCC / G++ and the target system is HPUX. My
testing environment (where I found the problem) is Cygwin. I suspect
that in HPUX I'm getting return value true as default, but on cygwin
I'm getting false.

I am trying to explain why this happen (to my self) and to justify
adding return false before the function exit. Appreciate any thought.

Because the program is ill-formed. A good compiler should issue a
diagnostic.
 
P

Paavo Helde

(e-mail address removed):
[...]
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
 
R

Ron Natalie

red said:
Because the program is ill-formed. A good compiler should issue a
diagnostic.

It's not ill-formed. It's just undefined behavior.
The compiler is not required to figure out if the code
flows off the end at compile time.
 
D

Dennis Handly

Syncnister said:
I have found some old C++ code with function defined as returning
boolean. The thing is that doesn't have any return statement at the
end. Looking at how the function is being used, suggesting that it is
expected to return true by default.

No, it will return whatever is in the return register, R8 for IPF and
R28 for PA.
I suspect that in HP-UX I'm getting return value true as default

This is all random as mentioned. It depends on opt level, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top