Thread: Return Pointer Deallocation

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Return Pointer Deallocation

    Well here's my silly doubt..

    Do i need to deallocate a return pointer...


    Code:
    char* function()
    {
    char *temp;
    temp=new char[100];
    
    
    \\ sometin
    
    
    
    return temp;
    
    }

    I cannot deallocate it in the function itself... SHould i deallocate after the function call in the main.



    Code:
    temp=function(); \\ considering temp is a pointer
    delete(temp);  \\ do i need to do this

    I am asking this because i have never deallocated this kind of pointer.. Is what i am doing wrong.. do i need to deallocate it .. if so what is the right method... (hope i am not balbbering)

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yes, you have to deallocate it. Stuff you allocate with new doesn't go out of scope when the function/block ends.
    Away.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by blackrat364
    Yes, you have to deallocate it. Stuff you allocate with new doesn't go out of scope when the function/block ends.

    For obvious reasons io cannot deallocate it in the function itself.. So is deallocating from the calling point a good enough method..

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yup, you can deallocate it anywhere.
    Away.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    First, the proper deletion would be:

    delete[] temp;

    You should, however, use something that automatically deallocates. For char *, use a std::string instead. For an array of any other type, use std::vector. If you need to return a new single object of some kind, something like boost::shared_ptr is the key.

    It's a very, very bad idea to write a function such that the caller must deallocate memory. If you must return a newly allocated resource, put it in a container that will free the memory automatically.
    Last edited by Cat; 08-06-2003 at 10:35 AM.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by Cat
    First, the proper deletion would be:

    delete[] temp;

    You should, however, use something that automatically deallocates. For char *, use a std::string instead. For an array of any other type, use std::vector. If you need to return a new single object of some kind, something like boost::shared_ptr is the key.

    It's a very, very bad idea to write a function such that the caller must deallocate memory. If you must return a newly allocated resource, put it in a container that will free the memory automatically.
    Thanx.. Could you please give me a small example.. Using Vector is one way... but how about if you have to use return pointers..

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Here are some examples:


    Code:
    Before using std::vector:
    
    #include <iostream>
    
    int * getArray(){
       return new int[25];
    }
    
    int main(){
       int * arr = getArray();
       for (int i = 0; i < 25; i++)
          arr[i] = i * 2;
       for (int i = 0; i < 25; i++)
          std::cout << "[" << arr[i] << "]";
       delete[] arr;
    }
    
    After using std::vector:
    
    #include <iostream>
    #include <vector>
    
    typedef std::vector<int> intArray;
    
    intArray getArray(){
       return intArray(25);
    }
    
    int main(){
       intArray arr = getArray();
       for (int i = 0; i < 25; i++)
          arr[i] = i * 2;
       for (int i = 0; i < 25; i++)
          std::cout << "[" << arr[i] << "]";
    }
    For single objects, you can use std::auto_ptr (NOT CONTAINER SAFE) or download Boost and use boost::shared_ptr (container safe). One example:

    Code:
    Before using a smart pointer:
    
    #include <iostream>
    
    struct Point{
       int x;
       int y;
    };
    
    Point * getPoint(){
       return new Point;
    }
    
    int main(){
       Point * p = getPoint();
       p->x = 3;
       p->y = 11;
       std::cout << "(" << p->x << ", " << p->y << ")" << std::endl;
       delete p;
    }
    
    After using a smart pointer:
    
    #include <iostream>
    #include <memory>
    
    struct Point{
       int x;
       int y;
    };
    
    typedef std::auto_ptr<Point> pointPtr;
    
    pointPtr getPoint(){
       return pointPtr(new Point); //Note how we wrap a call to new inside a constructor for the auto_ptr
    }
    
    int main(){
       pointPtr p = getPoint();
       p->x = 3;
       p->y = 11;
       std::cout << "(" << p->x << ", " << p->y << ")" << std::endl;
    }
    Hope that helps. The examples are contrived, but should illustrate usage.
    Last edited by Cat; 08-06-2003 at 11:38 AM.

  8. #8
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    thanx.. that sure did help..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM