Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Say we had this class</p> <pre><code>struct BigArray{ int operator[](size_t i)const{return m_data[i];} int&amp; operator[](size_t i){return m_data[i];} private: int m_data[10000000]; }; </code></pre> <p>And now say we had two instances:</p> <pre><code>BigArray a; a[0]=1;//initializaation etc BigArray b=a; </code></pre> <p>At this point we want this invariant</p> <pre><code>assert(a[0]==b[0]); </code></pre> <p>The default copy ctor ensures this invariant, however at the expense of deep copying the entire object. We may attempt a speedup like this</p> <pre><code>struct BigArray{ BigArray():m_data(new int[10000000]){} int operator[](size_t i)const{return (*m_data)[i];} int&amp; operator[](size_t i){return (*m_data)[i];} private: shared_ptr&lt;int&gt; m_data; }; </code></pre> <p>This will also meet the invariant, without making the deep copy, so all is good so far. Now using this new implementation we did </p> <pre><code>b[0]=2; </code></pre> <p>Now we want this to work the same as the deep copy case assert(a[0]!=b[0]); But it fails. To solve this we need a slight change:</p> <pre><code>struct BigArray{ BigArray():m_data(new int[10000000]){} int operator[](size_t i)const{return (*m_data)[i];} int&amp; operator[](size_t i){ if(!m_data.unique()){//"detach" shared_ptr&lt;int&gt; _tmp(new int[10000000]); memcpy(_tmp.get(),m_data.get(),10000000); m_data=_tmp; } return (*m_data)[i]; } private: shared_ptr&lt;int&gt; m_data; }; </code></pre> <p>Now we have a class that is shallow copied when only const access is needed, and deep copied when non-const access is needed. This is the idea behind the "shared_data" pointer concept. <strong>const</strong> calls will not deep copy (they call it "detach"), while non-const will deep copy if it is shared. It also adds some semantics on top of operator== so that it is not just comparing the pointer but the data as well, so that this would work:</p> <pre><code>BigArray b=a;//shallow copy assert(a==b);//true b[0]=a[0]+1;//deep copy b[0]=a[0];//put it back assert(a==b);//true </code></pre> <p>This technique is call COW (Copy on Write) and has been around since the dawn of C++. It is also extremely brittle -- the above example seems to work because it is small and has few use cases. In practice it is rarely worth the trouble, and in fact C++0x has deprecated COW strings. So use with caution. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload