Reputation: 533
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
Upvotes: 5
Views: 792
Reputation: 234715
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.
Upvotes: 6
Reputation: 8975
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.
Upvotes: 6
Reputation: 6555
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.
Upvotes: 1
Reputation: 1369
No, there will be no performance loss or memory leak. However, in general exceptions are the proper way of handling errors in C++.
Upvotes: 2
Reputation: 10864
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.
Upvotes: 16