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

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Turbo C++ Keyboard Shortcuts
S.No. Shortcuts keys Action 1. F1 For Help 2. F2 Save 3. F3 Open 4. F4 Go to cursor 5. F5 Zoom 6. F6 Next 7. F7 Trace...

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

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