The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Clean up problem
Discuss Clean up problem in the C Programming forum on Dev Shed. Clean up problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 25th, 2003, 05:03 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 13
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
|

May 25th, 2003, 05:07 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

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.
|

May 26th, 2003, 06:20 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 13
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.
|

May 27th, 2003, 08:47 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|