C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 25th, 2003, 05:03 AM
Nille Nille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 13 Nille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old May 25th, 2003, 05:07 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
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.

Reply With Quote
  #3  
Old May 26th, 2003, 06:20 AM
Nille Nille is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 13 Nille User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #4  
Old May 27th, 2003, 08:47 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
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/d...ings_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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Clean up problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap