title: Substraction or decrement random access iterator pointing to begin
body: <p>Consider following piece of code</p>
  
  <pre><code>void foo( bool forwad )
  {
      vector&lt;MyObject&gt;::iterator it, end_it;
      int dir;
  
      it = some_global_vector.begin() + some_position;
      if( forward )
      {
          dir = 1;
          it += 1;
          end_it = some_global_vector.end();
  
      }
      else
      {
          dir = -1;
          it -= 1;
          end_it = some_global_vector.begin()-1;
      }
  
      while( it != end_it )
      {
         if( do_domething() )
           break;
  
         it += dir;
      }
  }
  </code></pre>
  
  <p>As you can see there is some doubt when <code>forward == false</code> becouse there is an substraction from <code>begin()</code> and iterator <code>it</code> can be substracted when it points at <code>begin()</code>. I can't find anywhere if it is ok until I not dereference this bad pointing iterator).</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I read ISO C++ Standard and have some conclusions.
  There is no promise that <code>vector::begin()</code> can't internaly point to memory at adress <code>0</code>, and I was thinking that It is the end, but all containers depend on standard alocator. This alocator depends on <code>new</code> operator. And also, there is no information that <code>new</code> will never return <code>0</code>. But standard alocator depends also on <code>delete</code> operator and this operator is supose to do nothing if you pass <code>0</code>. So by this fact, <code>new</code> can't return <code>0</code> becouse there will be no way to delete that pointer, and by that, non empty <code>vector</code> can't return <code>begin()</code> that points to <code>0</code>.</p>
  
  <p><strong>Conclusion:</strong></p>
  
  <p>If above is right decrementing interator that points at <code>vector::begin()</code> should be safe, since internal memory of the <code>vector</code> is continouse.</p>
  
  <p>Am I right?</p>
  
  <p><strong>ULTIMATE ANSWER</strong></p>
  
  <p>Even if it works now and will be working in the future it is undefined behavour according to the standard. If you do this, you are doing this on your own risk. See this simmilar <strong><a href="https://stackoverflow.com/questions/6560606/on-what-architectures-is-calculating-invalid-pointers-unsafe">question</a></strong> for more informations.</p>