title: Is a forced integer buffer overflow legitime?
body: <p>I want to implement a Handletype like in this <a href="http://molecularmusings.wordpress.com/2013/05/17/adventures-in-data-oriented-design-part-3b-internal-references/" rel="nofollow">example</a>.</p>
  
  <p>(Long story short: the structure <code>Handle</code> holds an <code>index</code>-member to an array with elements. Its other member <code>count</code> validates if the <code>index</code> is up to date, corresponding to the datas <code>countArray</code>. <code>count</code> and <code>countArray</code> are with a fixed size of a type/bitfield( u32 : 20bits))</p>
  
  <p>To avoid being restricted to the 20bits of the generation/counter size, the following came into my mind: Why not let the <code>unsigned char count/countArray</code> overflow on purpose?
  I could also do the same with the modulo method <code>( counter = ++counter % 0xff )</code>, but that is another additional operation then..
  So let the count grow upto <code>0xff</code> and overflow will set it again to <code>0</code> when <code>0xff + 1</code> happens.
  Is this legitime?</p>
  
  <p>Here is my pseudo implementation (C++):</p>
  
  <pre><code>struct Handle
  {
          unsigned short index;
          unsigned char count;
  };
  
  struct myData
  {
          unsigned short curIndex;
          int* dataArray;
          unsigned char* countArray;
  
          Handle create()
          {
              // check if index not already used
              // create object at dataArray[handle.index]
              Handle handle;
              handle.index = curIndex;
              handle.count = countArray[curIndex]; 
              return handle;
          }
  
          void destroy( const Handle&amp; handle )
          {
              // delete object at dataArray[handle.index]
              countArray[handle.index]++; // &lt;-- overflow here?
          }
  
          bool isValid( const Handle&amp; handle ) const
          {
              return handle.count == countArray[handle.index];
          }
  };
  </code></pre>
  
  <p>EDIT #1: Yes, these integral types should all be unsigned (as indexes are)</p>