Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems you are misunderstanding pointer arithmetic</p> <pre><code>cb-&gt;buffer_end = (DataFragment *)cb-&gt;buffer + (capacity-1)*sz; cb-&gt;head = (DataFragment*)cb-&gt;head + cb-&gt;sz; cb-&gt;tail = (DataFragment*)cb-&gt;tail + cb-&gt;sz; </code></pre> <p>Pointer arithmetic already takes into account the size of the underlying type. All you really need is</p> <pre><code>++cb-&gt;head; ++cb-&gt;tail; </code></pre> <hr> <p>If the idea is to hack around <code>sizeof(DataFragment)</code> - perhaps to allocate more storage for one item than the struct's size - for some evil purpose - you'll need to first cast the pointer to a <code>char*</code> (because <code>sizeof(char) == 1</code>).</p> <pre><code>cb-&gt;tail = (DataFragment*)((char*)cb-&gt;tail + cb-&gt;sz); </code></pre> <hr> <p>Design-wise the struct appears to have too many members: <code>buffer_end</code> and <code>capacity</code> duplicate each other (given one you can always find the other), and the <code>sz</code> member is not necessary (it should always be <code>sizeof(DataFragment)</code>.</p> <p>Also, I believe you can just assign structs</p> <pre><code>*(cb-&gt;head) = *item; </code></pre> <p>there seem to be completely unnecessary casts (probably resulting from the misunderstanding of pointer arithmetic):</p> <pre><code>cb-&gt;buffer_end = (DataFragment *)cb-&gt;buffer + (capacity-1)*sz; </code></pre> <p>And if it is supposed to be C++, then it contains lots of "C-isms" (typedeffing structs, using <code>struct XXX var;</code> - despite having it typedeffed, etc), and the code is generally designed in a purely C style (not taking advantage of C++'s greatest strength, automatic resource management with RAII).</p> <hr> <p>May I also point out that <code>clock()</code> hardly gives you a <em>date</em> :)</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. 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