Freeing strings from class destructor

This is a discussion on Freeing strings from class destructor within the C++ Programming forums, part of the General Programming Boards category; I have class, but I don't know how I should handle freeing its member string, when I don't have a ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Freeing strings from class destructor

    I have class, but I don't know how I should handle freeing its member string, when I don't have a prior knowledge of how the string would get allocated. So say that I created a class C1,

    Code:
    class C1
    {
    public:
    c1() : str(NULL) {};
    ~c1() { delete [] str };
    
    char* str;
    };
    Client 1 uses the class in following way
    Code:
    int main(...)
    {
    C1 c1;
    c1.str = new char[256];
    strcpy(c1.str, "here is some string");
    return 0;
    }
    Client 2 uses the class in following way
    Code:
    int main(...)
    {
    C1 c1;
    c1.str = "here is some string";
    return 0;
    }
    Of course for the case of client 2, when the class object goes out of scope, its destructor would attemp to "delete []" the constant string which is illegal.

    How do you guys handle this kind of scenario? Is commenting to warn class users the only option, or would there other way of handling this programmatically?

    Would 1) create a set public function 2) always copy strings clients pass to the object 3) delete [] the copied string from destructor the only option?
    Last edited by chiefmonkey; 10-27-2009 at 04:05 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by chiefmonkey View Post
    I have class, but I don't know how I should handle freeing its member string, when I don't have a prior knowledge of how the string would get allocated.
    Unless you always allocate it yourself, you do not "own" it. Since you do not own it, you must never delete it.

    Ideally, you would be using a std::string, and have an accessor function that returns this value by reference. Now there is no issue of ownership, and the destructor of std::string takes care of everything for you automatically.

    Would 1) create a set public function 2) always copy strings clients pass to the object 3) delete [] the copied string from destructor the only option?
    That is essentially what I recommend above, but I wouldn't use a bare char *, I'd use a std::string so even the delete[] step goes away.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's indeed where encapsulation with clear ownership policies would help. But at the end of the day, it will not be possible to stop the user messing up.

    Personally I'd use std::string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Class destructor and throw
    By Unregisterred in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2002, 06:56 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

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