UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Copy-on-Write

Somenath Mukhopadhyay
21/03/2017 0 0

Note: You can read the original write-up at http://som-itsolutions.blogspot.in/2017/01/copy-on-write.html

As i was trying to recapitulate several important concepts of OOAD, naturally COW or Copy-On-Write got my attention for sometime. COW is a special technique used for efficient resource management in computer science. In C++ ‘98 standard, COW was used to define the std::string class. Although in C++ ‘11 standard, COW is not used to define the string class of the standard C++ library, but it is worth to have a look at the earlier implementation of the string class in GCC’s standard library. In COW, we defer creating of a new modifiable resource of a class in the case of copying if we keep the copy unchanged. We just refer to the old resource in the new copy. We write only when the copy changes that modifiable resource.

The following code is my effort to explain the COW concept in a String class. Let me explain some of the parts of this working code.

#include
#include

//This is using default constructor
//and copy constructor. Hence while copying it will
//use shallow copy.
class StringHolder{
public:
char* data;
int refCount;

virtual ~StringHolder(){
delete[] data;
}
};
class MyString{
private:
StringHolder* mStrHolder;
int length;

public:
//default constructor
MyString(){
mStrHolder = new StringHolder();
length = 0;
}

MyString(const char* inStr){
mStrHolder = new StringHolder();
unsigned l=strlen(inStr);
mStrHolder->data=new char[l+1];
memcpy(mStrHolder->data,inStr,l+1);
mStrHolder->refCount = 1;
length = l;

}

MyString(const MyString& inStr){
mStrHolder = inStr.mStrHolder;
mStrHolder->refCount++;
length = inStr.length;
}

//Reference counted assignment operator
MyString& operator=(const MyString& inStr){
//check against self assignment
if(this == &inStr)
return *this;


mStrHolder->refCount--;//the data of the lhs string
//does no longer point to the old char
//array
if(mStrHolder->refCount == 0)
delete mStrHolder;

mStrHolder = inStr.mStrHolder;//the lhs and rhs data
//both are pointing to the RHS data.
mStrHolder->refCount++;
length = inStr.length;
return *this;
}

//mutator service
MyString& operator+=(const MyString& inStr){
Detach();
const char* cStr = inStr.c_str();
int l  = inStr.length;
const char* oldData = mStrHolder->data;
//deep copy
char* tmp=new char[length + l];
mStrHolder->data = tmp;
memcpy(mStrHolder->data, oldData, length);
strcat(mStrHolder->data,cStr);
mStrHolder->refCount=1;
length+= l;
return *this;
}

//Destructor. The virtual term is important
virtual ~MyString(){
mStrHolder->refCount--;
if(mStrHolder->refCount == 0)
delete mStrHolder;
}
//Call this function first for all the
//mutator services of this class
void Detach(){
if (mStrHolder->refCount == 1)
return;
StringHolder* temp = new

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Data Structure: Searching
Searching Searching means finding an element in an array. There are two type of searching techniques : Linear Search Binary Search In linear search, to find the element array is traversed and...

Understanding Computer Science Concepts with Images and Videos..
All Computer science concepts relating to programming and software development are only virtual. It cannot be practically shown as a hardware parts of a computer. But for better understanding it should...

Lets learn why OOPS is popular?
1. Supports reusability- It means that once a class is designed it works just like a template which can be inherited. 2. Provides better management- It means that abstract classes and interfaces guide...
R

Do (pre & post)Increment and decrement operators behave differently when printed in the same and different line in C++?
Before I explain anything,I would like you to run the following program. void main() {int j =10; cout<<“j before increment =“<<j; cout<<“++j =“<< ++j; cout<<“...

Why do pointers have a datatype?
Before we start with pointers you must know what is a variable and a datatype. int a; This is the basic line in every program in 'C' . It means that we are asking the compiler to give us 2 bytes of space...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more