Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you have is that you haven't looked at copy construction and copy assignment: when you push an object into a <code>std::vector&lt;T&gt;</code> it gets copied and the objects with the <code>std::vector&lt;T&gt;</code> get possibly moved around using assignment. The default generated copy constructor and copy assignment just copy or assign the respective members, i.e., whenever either copy is used, you'd end up with two objects pointing to the same <code>buf</code>. The first one destroyed would use <code>delete[] buf;</code> and all others would have a stale pointer which can't be deleted again. That is, you want to add three methods to your <code>packetQueue</code>:</p> <pre><code>struct packetQueue { packetQueue(packetQueue const&amp; other); // copy constructor: copies the content packetQueue&amp; operator= (packetQueue const&amp; other); //copy assignment: updates the content ~packetQueue() // destructor: release the memory void swap(packetQueue&amp; other); // swap the content // other members }; </code></pre> <p>To leverage the copy construction and destruction in the copy assignment, I find it useful to have a <code>swap()</code> member, because this is easily implemented and makes for a nice, simple copy assignment:</p> <pre><code>void packetQueue::swap(packetQueue&amp; other) { std::swap(this-&gt;buf, other.buf); std::Swap(this-&gt;size, other.size); } packetQueue&amp; packetQueue::operator= (packetQueue const&amp; other) { packetQueue(other).swap(*this); return *this; } </code></pre>
    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. 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