5

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

Martin
  • 493
  • 6
  • 16

6 Answers6

16

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.

PP.
  • 10,764
  • 7
  • 45
  • 59
  • Thanks, Can you provide some source for that too? because I was not able to find any on the web :( – Martin Sep 23 '13 at 13:57
  • Unfortunately most of my C knowledge comes from reading K&R many years ago and pouring over assembly generated from C compilers. By and large built-in types are returned through a register (e.g. `AX`) and thus ignoring the value is safe in that it doesn't cause a leak. I'm not so sure about returning structures (which I believe is legal) because I don't know off the top of my head how they are returned... that is why I'm not prepared to give an authoritative answer *how* at this stage... – PP. Sep 23 '13 at 14:01
  • 1
    @Zaibis [this question implies that you can](http://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c) and I'm sure I've done it somewhere in my past... – PP. Sep 23 '13 at 14:03
  • 1
    @Zaibis, it perfectly legal to return structures, it's not legal to return an array (a pointer to an array is fine). – john Sep 23 '13 at 14:04
  • As all the questions seem to have pretty much the same answer and the community seem to be of you opinion, i decide to tag yours as the right one, but if someone may provide a source for that, i would be glad to see it :D – Martin Sep 23 '13 at 14:04
  • 1
    @PP. If I remember right a popular way to implement return values in assembly code is to reserve some space on the stack "below" the scope of the function to be called. I.e. the function writes below its own stack scope the return value (which is uninitialized if there is no `return` statement being executed) – leemes Sep 23 '13 at 14:05
  • 1
    @Martin actually [this Wikipedia article](http://en.wikipedia.org/wiki/C_%28programming_language%29) states you can ignore function return values. – PP. Sep 23 '13 at 14:06
6

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.

nikolas
  • 8,707
  • 9
  • 50
  • 70
6

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 3
    Returning a file handle is a good "gotcha" because it is passed around as an integer on many file systems - and should be closed with a corresponding close function cleanly. – PP. Sep 23 '13 at 14:11
  • And even when it's passed as a pointer (i.e. `FILE*`, you still shouldn't call `free` or `delete` but `fclose`. – MSalters Sep 24 '13 at 08:13
2

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

cdoubleplusgood
  • 1,309
  • 1
  • 11
  • 12
2

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

Filip
  • 656
  • 4
  • 8
1

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.

dhein
  • 6,431
  • 4
  • 42
  • 74