title: Reference invalidation after applying reverse_iterator on a custom made iterator
body: <p>I implemented a bidirectional iterator, however instead of operating on a data structure, it returns a mathematical series which one can iteratively calculate through in both directions. In fact, I'm iterating through the integers, using ++ and -- on an int. This means that the data is not stored in a different structure, and hence when the iterator goes out of scope, so does the value.</p>
  
  <p>Nevertheless, I would expect the next code (minimal failing example) sample to work, as the iterator stays in scope the whole time. But it does not work :(</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;vector&gt;
  
  class my_iterator : public std::iterator&lt;std::bidirectional_iterator_tag, int&gt; {
    int d_val = 12;
  public:
    my_iterator  operator--(int) { std::cout &lt;&lt; "decrement--\n"; return my_iterator(); }
    my_iterator &amp;operator--()    { std::cout &lt;&lt; "--decrement\n"; return *this; }
    my_iterator  operator++(int) { std::cout &lt;&lt; "increment++\n"; return my_iterator(); }
    my_iterator &amp;operator++()    { std::cout &lt;&lt; "++increment\n"; return *this; }
  
    int &amp;operator*() { std::cout &lt;&lt; "*dereference\n"; return d_val; }
  
    bool operator==(my_iterator const  &amp;o) { return false; }
    bool operator!=(my_iterator const  &amp;o) { return true ; }
  };
  
  
  int main() {
    auto it = std::reverse_iterator&lt;my_iterator&gt;();
    int &amp;i = *it;
    if (true)
    {
      std::cout &lt;&lt; i &lt;&lt; '\n';
    }
    else
    {
      std::vector&lt;int&gt; vec;
      vec.push_back(i);
      std::cout &lt;&lt; vec[0] &lt;&lt; '\n';
    }
  }
  </code></pre>
  
  <p>source: <a href="http://ideone.com/YJKvpl" rel="noreferrer">http://ideone.com/YJKvpl</a></p>
  
  <p>The if-branch results in memory violations, as properly detected by valgrind:</p>
  
  <pre><code>--decrement
  *dereference
  ==7914== Use of uninitialised value of size 8
  ==7914==    at 0x4EC15C3: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4EC16FB: std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::_M_insert_int&lt;long&gt;(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4EC1C7C: std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::do_put(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4ECEFB9: std::ostream&amp; std::ostream::_M_insert&lt;long&gt;(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x40087B: main (divine.cc:25)
  ==7914== 
  ==7914== Conditional jump or move depends on uninitialised value(s)
  ==7914==    at 0x4EC15CF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4EC16FB: std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::_M_insert_int&lt;long&gt;(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4EC1C7C: std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::do_put(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4ECEFB9: std::ostream&amp; std::ostream::_M_insert&lt;long&gt;(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x40087B: main (divine.cc:25)
  ==7914== 
  ==7914== Conditional jump or move depends on uninitialised value(s)
  ==7914==    at 0x4EC1724: std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::_M_insert_int&lt;long&gt;(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4EC1C7C: std::num_put&lt;char, std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; &gt;::do_put(std::ostreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;, std::ios_base&amp;, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x4ECEFB9: std::ostream&amp; std::ostream::_M_insert&lt;long&gt;(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
  ==7914==    by 0x40087B: main (divine.cc:25)
  ==7914== 
  12
  ==7914== 
  ==7914== HEAP SUMMARY:
  ==7914==     in use at exit: 0 bytes in 0 blocks
  ==7914==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
  ==7914== 
  ==7914== All heap blocks were freed -- no leaks are possible
  ==7914== 
  ==7914== For counts of detected and suppressed errors, rerun with: -v
  ==7914== Use --track-origins=yes to see where uninitialised values come from
  ==7914== ERROR SUMMARY: 5 errors from 3 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>The else-branch doesn't result in memory violations, or at least as far as my valgrind can detect. However, the value stored in the vector is 'random':</p>
  
  <pre><code>--decrement
  *dereference
  -16777520
  </code></pre>
  
  <p>I'm a bit surprised by what happens. The iterator should be in scope all the time, yet the reference seems to get invalidated. Why do I get the memory violations, whilst 12 is printed or why don't I get them whilst something different from 12 is stored?</p>