Introduction: I got a function doing some work and returning a bool. The bool should be false, if some error occured. So I could use the bool, if I want to check for errors. But what, if I am completely sure (I know, you can never be completely sure, but maybe you understand what I mean) that there will be no error in this part or you just don't care if there is because it has no influence.
Question: Will there occure some memory leak or other performance issue if I just not 'catch' the returned bool?
minimum Codeexampe:
bool my_func(/*some variables*/)
{
if(/*error-condition*/)
{
//do something
return false;
}
else if(/*other error-condition*/)
{
//do something
return false;
}
return true;
}
int main(void)
{
my_func(/*variables*/);
return 0;
}
Comment: that will not return any compiling or runtime errors or unhandled exceptions
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.
It is mainly to increase readability and to help future programmers to understand what you were trying to do. There should be no assumption about what a function returns.
Function with no argument and no return value: When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function.
0 always means success, but other exit codes are also permitted to mean success depending on the system.
You can safely ignore the return value of a function if it is not a pointer to memory that was allocated from within the function and not freed within that function.
In C++ memory can only leak for objects with dynamic storage duration, that is, objects allocated with new
or std::malloc
(cmp. C++11 3.7.4/1)
The bool
in your function will be a temporary, or in other words, it has automatic storage duration (cmp. C++11 3.7.3/1), so it is perfectly fine to ignore that.
Function return values can always be ignored unless they tell you information about resources that a function has consumed and not released; e.g.
1) A pointer or a structure containing pointers to memory allocated by the function
2) A pointer or a structure containing pointers to files / stream buffers opened by the function
You should always check the function documentation before choosing to ignore a return value.
No, there will be no performance loss or memory leak. However, in general exceptions are the proper way of handling errors in C++.
No, it is a void statement, the bool is discarded
You can feel free to ignore it as the return value just doesn't gets used.
It is just a void statement this would be at all the same as writing 2;
or identifiername;
In your code.
It would get invoked, but as you aren't storing the value, nothing else will happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With