473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Absent of return in old C++ codes

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.
Mar 9 '08 #1
4 1297
Syncnister wrote:
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.
Mar 9 '08 #2
Syncnister <ha*********@gmail.comwrote in news:b6e82241-d3e3-4764-8384-
5f**********@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
Mar 9 '08 #3
red floyd wrote:
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.
Mar 10 '08 #4
Syncnister wrote:
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.
Mar 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the...
2
by: lian ravago | last post by:
How to get the num of days absent using c# when start date and end date are the date format is like this 12/24/12 *** Sent via Developersdex http://www.developersdex.com *** Don't just...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
16
by: Fao, Sean | last post by:
As far as I can tell, the standard has defined three portable return codes from function main() (0, EXIT_SUCCESS, EXIT_FAILURE). Personally, on all platforms I have worked with, EXIT_SUCCESS is,...
3
by: c# beginner | last post by:
we are trying to standardize return codes across our .NET applications (that are soon to be developed.) What is the best practice for standardizing return codes? I know of only the following...
3
by: RdS | last post by:
Where do I find return codes for MS methods. For instance the filesystemobject methods. I need to know what methods return (error codes, etc. and what they mean). I have searched MSDN and of...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
3
by: asearle | last post by:
Hallo everyone, I have a perl script which can return error codes. I have done this with, for example, 'exit 11;', which would (I imagine) send the exit code 11 (instead of 0) back to the calling...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.