title: Array operator using address outside of array
body: <p>I recently came across a C++ class that implements a non zero index based array. I.e. one could initialize this class to have a valid index range of say 15..19. This class calculates the adress for the element with index 0, even though this probably is outside of allocated address space and stores that adress. Element access is then just handled via operator[] using this element 0 address. Some example code (simplified):</p>
  
  <pre><code>template &lt;typename T&gt;
  class NonZeroArray
  { 
  
  public: 
  
  NonZeroArray(int min_index, int size, T default_element) : 
      m_min_index(min_index), 
      m_size(size),
      m_default_element(default_element)
  {
      m_base_address = (T*) malloc(size*sizeof(T));
      m_zero_index_adress = m_base_address - min_index;
  }
  
  ~NonZeroArray()
  {
      free(m_base_address);
  }
  
  T&amp; element(int index) 
  {
      if ( index &lt; m_min_index || index &gt;= m_min_index + m_size) 
          return m_default_element;
      else
          return m_zero_index_adress[index];
  } 
  
  private:
      int m_min_index;
      int m_size;
      T* m_base_address;
      T* m_zero_index_adress;
      T m_default_element;
  };
  </code></pre>
  
  <p>This seems unneccessarily complicated to me. I really don't see why the developer didn't chose to implement element access via m_base_address[index+m_min_index]. But worse, I am worried that this current implementation could be dangerous. The code seems to work reliably but I was wondering what would happen if there is an arithmetic overflow in the calculation of m_zero_index_address, i.e. if the minimum index offset is greater than the allocatead address. Is there a chance for this to go horribly wrong or is this code always safe?</p>