Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me repeat your question: "My question, how cyclic data structures makes reference count above zero, kindly request to show with example in C++ program. How the problem is solved by <code>weak_ptrs</code> again with example please."</p> <p>The problem occurs with C++ code like this (conceptually):</p> <pre><code>class A { shared_ptr&lt;B&gt; b; ... }; class B { shared_ptr&lt;A&gt; a; ... }; shared_ptr&lt;A&gt; x(new A); // +1 x-&gt;b = new B; // +1 x-&gt;b-&gt;a = x; // +1 // Ref count of 'x' is 2. // Ref count of 'x-&gt;b' is 1. // When 'x' leaves the scope, there will be a memory leak: // 2 is decremented to 1, and so both ref counts will be 1. // (Memory is deallocated only when ref count drops to 0) </code></pre> <p>To answer the second part of your question: It is mathematically impossible for reference counting to deal with cycles. Therefore, a <code>weak_ptr</code> (which is basically just a stripped down version of <code>shared_ptr</code>) <strong>cannot</strong> be used to solve the cycle problem - the programmer is solving the cycle problem.</p> <p>To solve it, the programmer needs to be aware of the <strong>ownership</strong> relationship among the objects, or needs to invent an ownership relationship if no such ownership exists naturally.</p> <p>The above C++ code can be changed so that A owns B:</p> <pre><code>class A { shared_ptr&lt;B&gt; b; ... }; class B { weak_ptr&lt;A&gt; a; ... }; shared_ptr&lt;A&gt; x(new A); // +1 x-&gt;b = new B; // +1 x-&gt;b-&gt;a = x; // No +1 here // Ref count of 'x' is 1. // Ref count of 'x-&gt;b' is 1. // When 'x' leaves the scope, its ref count will drop to 0. // While destroying it, ref count of 'x-&gt;b' will drop to 0. // So both A and B will be deallocated. </code></pre> <p>A crucial question is: Can <code>weak_ptr</code> be used in case the programmer cannot tell the ownership relationship and cannot establish any static ownership because of lack of privilege or lack of information?</p> <p>The answer is: If ownership among objects is unclear, <code>weak_ptr</code> <strong>cannot</strong> help. If there is a cycle, the programmer has to find it and break it. An alternative remedy is to use a programming language with full garbage collection (such as: Java, C#, Go, Haskell), or to use a conservative (=imperfect) garbage collector which works with C/C++ (such as: Boehm GC).</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.
    3. 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