What (actually) happens, when a function with the warning control reaches end of non-void function is called?
A: No, the missing return would not cause stack corruption
A: Yes, the behavior would be "undefined" if the caller tried to read and/or use the (undefined!) return value.
PS:
Here's a citation for C++:
C++03 §6.6.3/2:
Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning
function.
Why do I get the warning 'control reaches end of non-void function' even when the return is guaranteed
The compiler correctly assumes that r
is a variable value, it does not process it as a constant, that being the case it cannot be sure what the value is going to be and therefore if the body of the cycle will or will not be executed.
If you use a constant the warning goes away because the compiler will assemble the code using a constant value.
You can check this behavior here.
warning: control reaches end of non-void function [-Wreturn-type] removed when declaring method inline
In contrast to a non-inline function with external linkage, an inline function or a function with internal linkage (static
) is guaranteed to be defined in every translation unit (odr-)using the function (or else the program is ill-formed).
The compiler is therefore just being "lazy" and doesn't emit a definition for the inline function, since you don't (odr-)use it in the translation unit.
If you (odr-)use it in any way, it will be emitted and the checks wont be skipped. It is enough to take a pointer to the function:
auto ptr = &enum_to_int;
And then the warning will appear again.
It is probably reasonable that the compiler doesn't check the function for such issues in a translation unit where the definition doesn't need to be emitted. It is guaranteed that the program either doesn't use the function at all, in which case the problem doesn't really matter, or it is (odr-)used in another translation unit and the warning will be produced there.
Checking it in all translation units would probably just be wasted time.
What does control reaches end of non-void function mean?
The compiler cannot tell from that code if the function will ever reach the end and still return something. To make that clear, replace the last else if(...)
with just else
.
Cannot understand error: warning: control reaches end of non-void function [-Wreturn-type]
As Elliott in the comments has said the warning you are getting is not going to affect your program at all. It's recommend that you return something since you function has a return type of integer.
Below is a easy fix to get rid of the warning. :)
Old Code:
if (a == 1)
{
std::cout << "Its a prime number\n";
}
New Code without the Warning:
if (a == 1)
{
std::cout << "Its a prime number\n";
return 0;
}
Warning: control reaches end of non void function
You can try this, for example:
bool check_table_full (char board[][SIZE])
{
int row, col;
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
if (board[row][col] != '_') {
return true;
} else {
return false;
}
}
}
return false;
}
How to solve the control reaches end of non-void function warning?
The control reaches end of non-void function
warning occurs when that function return type is not void, but the function can reach the end without a return
.
It can be caused by control statements such as if-statements and missing return statements.
To answer "I specified the return value of the function as void but I am getting an error",
- Your function
osa_odm_init
returns aRC_Code_t
, notvoid
. Thevoid
is in the arguments, indicating no arguments.
The actual cause is that it returns RC_Code_t
, but the return
is only here if the if-statement is true, you are missing the return
if the if-statement fails. The edited code should be
extern RC_Code_t osa_odm_init (void)
{
if ( odmInitFlag == BOOL_FALSE )
{
........
........
return (RC_OK);
}
// This section runs if ( odmIntFlag != BOOL_FALSE )
// In your original code, you omitted the return
return RC_ERROR; // Edit: Or return another RC_Code_t result
}
Why a warning of control reaches end of non-void function for the main function?
Just put return 0
in your main()
. Your function main returns an int (int main(void)
) therefore you should add a return in the end of it.
Control reaches the end of a non-void function
Problem: I received the following warning:
warning: control reaches end of non-void function
Solution: This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.
source
Solution:
int main(void)
{
my_strcpy(strB, strA);
puts(strB);
return 0;
}
Related Topics
Constexpr Initializing Static Member Using Static Function
How Boost::Function and Boost::Bind Work
When Should I Use Typedef in C++
Capturing Perfectly-Forwarded Variable in Lambda
In C++11, Does 'I += ++I + 1' Exhibit Undefined Behavior
How to Return in Void Function
What Is the Maximum Length in Chars Needed to Represent Any Double Value
Is There Any Compiler Barrier Which Is Equal to Asm("" ::: "Memory") in C++11
Hash an Arbitrary Precision Value (Boost::Multiprecision::Cpp_Int)
G++ Does Not Show a 'Unused' Warning
Std::String Length() and Size() Member Functions
How to Get the Gl Library/Headers
How to Safely Use Openmp with C++11
What Is the Scope of a 'While' and 'For' Loop
Efficient Bitwise Operations for Counting Bits or Find the Right|Left Most Ones