33

i'm trying to come up with a concrete reasoning why to use a pointer over a reference as a return type from a function ,

my reasoning is that if inadvertently a null value is returned to a reference type it could not be checked , and could lead to run-time errors

  int& something(int j)
  {
      int* p = 0 ;  
      return *p;
  }

  void main()
  {
      int& i = something(5);
      i = 7;   // run-time error 
  }

if i had used a pointer i could check it and avoid the error the pointer return value would act as a contract to that a value must be returned.

 void main()
 {
         int* i = something(5);
         if( i != null )
             *i = 7;
 }

any thoughts would be appreciated again ,

what would you use and why reference or pointer

thanks in advance.

8
  • 8
    You should never return a reference to local memory anyway.
    – Chad
    Oct 18, 2011 at 21:10
  • 1
    Why would you do either of those things? Oct 18, 2011 at 21:10
  • 3
    @Chad: There's no prima facie indication that the reference would be to local memory. As it stands, the code simply returns a null pointer. Oct 18, 2011 at 21:13
  • 3
    @AJG85 The code is just exemplifying what could go wrong when returning a reference (and it's obviously contrived in order to be simple and to the point). The question is not about fixing the code that is incorrect for example purposes. Oct 18, 2011 at 21:23
  • 1
    @AJG85: The whole thrust of the question is whether the risk of inadvertently returning a "null reference" suggests that a pointer is preferable. It is obvious that the example function is deliberately doing the wrong thing just to illustrate the point. Plus, I have no idea what any of this has to do with my comment about returning a reference to local memory. Oct 18, 2011 at 22:29

6 Answers 6

35

You could use a pointer instead of a reference if:

  • Null is a valid return value
  • You dynamically constructed something in the function, and the recipient becomes the owner. (In this case, you might consider returning a smart pointer such as std::unique_ptr or boost::shared_ptr.)

Regardless, you would not want to return either a pointer or a reference to a local variable.

2
  • 4
    Just for the people reading this (>2016) since 2011 it seems auto_ptr has been deprecated. unique_ptr would be more fitting now. Mar 18, 2016 at 1:32
  • @andersfylling - Fixed. Thanks! It had been deprecated for two months when I mentioned it. Feb 3, 2020 at 14:43
21

References are a different way of thinking. Think of references as "pointers to existing objects". Once you do that, you'll understand why they can't be NULL - the object exists and the reference points to it.

Therefore, if your function returns a reference to something that it creates, it needs to guarantee that it actually does create a valid object. If it does not, or is unable to, then that is grounds to throw an exception.

Contrast that with a pointer. A pointer can be NULL and the caller will have to deal with a NULL return value. Therefore, if your function cannot guarantee that it will return a valid reference and you don't want to throw exceptions, you will need to use pointers.

3

If you inadvertently return a null value, that's a bug. You can just as easily place the check inside something() and throw an exception if it's null.

Having said that, the historical convention is to return heap objects via pointers, even if they are guaranteed to be non-null.

1

If your function is intended to "always" return a value, then your function should return a reference.

In those cases where, for some exceptional reason, you cannot find the value to return, you should throw an exception.

You should not rely on there being a run-time error generated when you try to return a reference to a null or wild pointer. The behavior is undefined. Anything could happen.

1

C++ references cannot be null. The bug is dereferencing a null pointer. That's undefined behaviour.

2
  • Here's a null C++ reference: void print_value(int &v) { printf("0x%08x\n", &v); printf("%d\n", v); } int main() { int *d = 0; print_value(*d); }
    – moof2k
    Dec 30, 2017 at 1:22
  • @moof That program is UB: stackoverflow.com/a/2727872/505088 Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. Dec 30, 2017 at 5:03
0

It is usually dependent on what you are trying to accomplish, but I generally treat return values this way:

Return pointer - Caller owns memory (and cleanup)

Reference - Callee owns the memory. DO NOT return a dynamically allocated point unless the callee manages it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.