Returning Pointers

This is a discussion on Returning Pointers within the C++ Programming forums, part of the General Programming Boards category; This always is a confusion for me. When is it safe and not safe to return reference to a pointer?...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Returning Pointers

    This always is a confusion for me.

    When is it safe and not safe to return reference to a pointer?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Not sure if I understand the question. It should be safe depending on what you want to do.
    Returning a reference to a pointer is not something you will do often. It is used when you want to return a static variable declared inside the function.

    Code:
    //Counts how many times the function was called and skips count by x.
    int*& SkipCounter(int x = 0) {
        static int counter = 0;
        ++counter += x;
        static int* value = &counter;
        return value;
    }
    
    int main() {
        int test = *SkipCounter(); // test = 1. It was called the first time
        int test2 = *SkipCounter(1); // test = 3, previous count + 1 + skip of 1 
        int test3 = *SkipCounter();  //test = 4, previous count + 1
        int test4 = *SkipCounter(2);  //test = 7, previous count + 1 + skip of 2
    }
    Sorry if I couldn't come up with a better example. But honestly couldn't think of a good one where it may make sense to use a function that returns a reference to a pointer.

    All in all, the whole thing could be written with a simple return of int.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Just combine the rules of when it is unsafe to return a reference to any type with the rules for when it is unsafe to return a pointer, and you have the rules for when it is unsafe to return a reference to a pointer. For example, it is unsafe to return a reference to any type if the object (or pointer) will go out of scope when the function ends. It is also unsafe to return a pointer if the pointer points to memory that will be destroyed when the function ends. So if the pointer points to an object about to be destroyed, or if the pointer itself is aboutto go out of scope, then it is unsafe to return it.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    41
    Thanks for the response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 05:25 AM
  2. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 10:16 AM
  3. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21