CPP Dangerous Reference Handling

Returning Out-of-Scope Object References.

  • Reference is always an alias to an object so passing it as argument to a function or method should not be any problem.
  • However, what happen if you try to return a reference of an object, which was created inside the method or the function?
  • This is a dangerous situation as such an object will no longer exists when the function or method are finish with their tasks.
    Example of a dangerous return of an object reference from a method:
    // Returning a reference to an object which no longer exists
    #include <iostream>
    class Person {
    public:
      Person () {
      }
      Person (int age) {
        this->age = age;
      }
      ~Person() {}
      int getAge() { return age; }
      Person & getNewPerson(int age) {
        Person person(age);
        return person;
      }
    private:
      int age;
    };
    
    int main() {
      Person * pPerson= new Person();
      Person & Ricard = pPerson->getNewPerson(50);
      // the reference of the returned object
      // has no object reference as the object is now out of scope
      int age = Ricard.getAge();
      std::cout << "Ricard is " << age << " years old!" << std::endl;
      return 0;
    }
    When we run this application the result could will be (or may be the compiler fails):
    Ricard is -858993460 years old!

    You can download this example here (needed tools can be found in the right menu on this page).

  • You may be thing you could overcome this problem with creating the object on the free store and return the reference of that.
  • Well it will work but you got a new problem to solve:
    Returning an object reference from a method, where the object exists on the free store:
    // Returning a reference to an object created on the free store
    #include <iostream>
    class Person {
    public:
      Person () {
      }
      Person (int age) {
        this->age = age;
      }
      ~Person() {}
      int getAge() { return age; }
      Person & getNewPerson(int age) {
        Person * pPerson= new Person(age);
        return *pPerson;
      }
    private:
      int age;
    };
    
    int main() {
      Person * pPerson= new Person();
      Person & Ricard = pPerson->getNewPerson(50);
      // the reference of the returned object
      // has no object reference as the object is now out of scope
      int age = Ricard.getAge();
      std::cout << "Ricard is " << age << " years old!" << std::endl;
      // Now you should delete the location on the free store
      Person * pRicard= &Ricard;
      delete pRicard ;
      // Every thing work find BUT Ricard refer now to what???
      return 0;
    }
    When we run this application, the result will be:
    Ricard is 50 years old!

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.