Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I get a performance issue, if i do not care about a return value

Tags:

c++

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

like image 771
Martin Avatar asked Sep 23 '13 13:09

Martin


People also ask

What happens if you don't return value from function?

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.

Why is return value important?

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.

What does it mean to not return a value?

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.

Is return code 0 successful?

0 always means success, but other exit codes are also permitted to mean success depending on the system.


6 Answers

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.

like image 117
PP. Avatar answered Nov 15 '22 11:11

PP.


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.

like image 21
nijansen Avatar answered Nov 15 '22 13:11

nijansen


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.

like image 39
Bathsheba Avatar answered Nov 15 '22 11:11

Bathsheba


No, there will be no performance loss or memory leak. However, in general exceptions are the proper way of handling errors in C++.

like image 28
cdoubleplusgood Avatar answered Nov 15 '22 13:11

cdoubleplusgood


No, it is a void statement, the bool is discarded

like image 27
Filip Avatar answered Nov 15 '22 11:11

Filip


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.

like image 23
dhein Avatar answered Nov 15 '22 12:11

dhein