Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes you can trust vectors to clean up after themselves.</p> <p><strong>HOWEVER</strong> You cannot trust the stuff vector holds to cleanup after itself. What needs to be cleaned up could be something that persists outside of your application. If its memory, this isn't a worry. If its making sure the XML tags are all closed, then the OS isn't going to be able to help you.</p> <p>For example, what if you have a vector of some wonky lock object like this:</p> <pre><code> class CLock { public: CLock() {} ~CLock() {} void Lock(...) {...} void Unlock(...) {...} }; std::vector&lt;CLock&gt; myLockVec; </code></pre> <p>How would your vector of CLock's know to unlock everything when its done? Vector's aren't built to know about locks.</p> <p>This is essentially the same situation as having a vector of pointers:</p> <pre><code> std::vector&lt;int*&gt; myIntVec; </code></pre> <p>How does the vector know which pointers here have been deleted and NULL'd, and which ones are really there? Maybe some have been deleted and set to your special value 0xdeadbeef, meaning deleted.</p> <p>The point is the vector has no means to know this or know that its elements are pointers or locks or whatever. They just need to be things that have default constructors and are copyable, and meet the other such requirements that vector has on its elements.</p> <p>The solution is to be sure that whatever vector HOLDS needs to be responsible for its cleanup. This is called <strong>RAII</strong> -- Resource Allocation Is Initialization, more importantly here, Resource Destruction is Deallocation. With Our CLock example above, the answer is obvious, be sure to unlock when we're done!</p> <pre><code> class CLock { ... ~Clock() { if (locked) { Unlock(); } } } </code></pre> <p>But with pointers its not so obvious. The solution is to wrap up the pointer in a smart_ptr class. The most prolific of these are the <a href="http://www.boost.org/doc/libs/1_39_0/libs/smart_ptr/smart_ptr.htm" rel="nofollow noreferrer">boost family of smart poniters</a>.</p> <pre><code>class CSmartPointer&lt;T&gt; { CSmartPointer( T* rawPtr) { m_ptr = rawPtr; } ~CSmartPointer() { delete m_ptr; } } </code></pre> <p>Additional features are brought into play with pointers such as reference counting, but the above example should give you the gist of the nature of the problem and how its typically solved.</p>
    singulars
    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.
    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