UrbanPro
true
default_background

Learn C++ Language from the Best Tutors

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

Search in

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

What is Abstraction?
In theortical terms, abstraction is: Hiding details from the outer world. Providing only essential details to outer world. C++ provide ways to hide details using access specifiers: public, protected...

C++ Fundamentals - Introduction
Introduction to C++ C++ CHARACTER SET It is the set of all valid characters that a language can recognize . It represents any letter, digit, or any other sign .C++ character set: LETTERS...

Why Indexing Should Start From Zero In Array ?
Why numbering should start at zero? To denote the subsequence of natural numbers 2, 3, ..., 12 without the pernicious three dots, fourconventions are open to usa) 2 ≤ i < 13b) 1 < i ≤ 12c)...

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

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