#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    13
    Rep Power
    0

    Clean up problem


    Hi,
    I'm making a String class for educational purpose (new to c++). My problem is that multiple strings can reference the same buffer, to keep the string as light as possible. This means I have a copy constructor that looks like this:

    Code:
    String::String(const String& str)
    {
       this->length = str.length;
       this->data    = str.data;
    }
    The problem is when the string is destructed. I don't know how the buffer has been created. It could have created using malloc, in which case I should invoke free, or it could have been created from a simple char array. So how can I free the buffer?

    The other problem is that I don't know if the buffer is referenced from another string. If the buffer would be freed, every string referencing it would be corrupted. How can I know when to free the buffer? Sort of like this:
    Code:
    String::~String()
    {
       if(numberOfReferencesToBuffer == 0)
       {
          if(isBufferOnHeap(buffer))
          {
              free(buffer);
          }
       }
    }
    Thanks in advance,
    Nille
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    If sounds like horrible program design to me.

    Is the "buffer" an object of your string class? If it is, you could create another variable in your string class to increment a counter everytime another string makes a copy of the string. Then only destroy a string object if the counter is zero. If it's a separate entity, you can still set up a similar variable or array of variables for multiple buffer counters. You're going to have to deal with the complexity of making a copy of a string that is a copy of the buffer: they'll all point to the same area in memory.

    As for how you will know whether to free up memory for your buffer, you could have a flag variable that you set to true or 1 to indicate that you had to allocate memory for it.
    Last edited by 7stud; May 25th, 2003 at 05:14 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    13
    Rep Power
    0
    If sounds like horrible program design to me.
    It looks better in my head :) The concept is that the String is an immutable "view", if you will, of the underlying character buffer. Since I come from Java, I am used to using resources carefully. I usually pass references, but in the case of strings it's sometimes easier to just pass it as a copy (I overload const char*). I just want to copy the "view", not that data. It looks like this inside my head:

    Code:
         _____buffer_______
        |        |         |
    String    String    String
    I just need to know how to delete the buffer when it is no longer referenced. I posted a new topic concerning memory. I hope I will get a response there.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Sounds like you're trying to implement a reference counted string implementation. IIRC, the ANSI standards committee specified this in one of their standards, and some implementations of the STL *do* use reference counted strings. So does Microsoft's CString class incidentally (proof here: http://msdn.microsoft.com/library/de...ngs_in_mfc.asp)

    To see some examples, see these links:
    http://www.michaelmoser.org/string.htm (gives you some reference counting basics)
    http://www.gotw.ca/gotw/043.htm (3 part series)
    http://www.cse.ucsc.edu/~pohl/Winter01/LL4/ (look to the bottom of the page)

    To see some problems with various implementations, see:
    http://www.sgi.com/tech/stl/string_discussion.html

    IIRC, there was a discussion about problems in some string implementations in Dr. Dobbs Journal, a while ago. Either that, or this Newcastle beer is really beginning to kick in :).

    Hope this helps :)

IMN logo majestic logo threadwatch logo seochat tools logo