title: Python Exception: index out of range while debuging the code c++ with support for stl with gdb
body: <p>I have a code:</p>
  
  <pre><code>#include &lt;list&gt;
  
  int f(std::list&lt;int&gt;&amp; l)
  {
      l.clear();
      int i = 0;
      return i;
  }
  
  int main(int argc, char* argv[])
  {
      std::list&lt;int&gt; l;
      int i = f(l);
      i++;  
  }
  </code></pre>
  
  <p>I build it in 3 ways:</p>
  
  <ol>
  <li>g++ -g -o main1 ../main1.cpp</li>
  <li>g++ -g -o main1 -O1 ../main1.cpp </li>
  <li>g++ -g -o main1 -O2 ../main1.cpp</li>
  </ol>
  
  <p>I debug it with gdb (GNU gdb (GDB) 7.5.1) and when I step into int f(std::list&amp; l) I get such output:</p>
  
  <pre><code>Python Exception &lt;type 'exceptions.IndexError'&gt; list index out of range:
  (gdb) Python Exception &lt;type 'exceptions.IndexError'&gt; list index out of range:
  </code></pre>
  
  <p>Here is my gdb session:</p>
  
  <pre><code>(gdb) bre main
  Breakpoint 1 at 0x400603: file ../main1.cpp, line 11.
  (gdb) r
  Starting program: /home/mhd/Texts/Programming/Programms/Exercises/Linux/BruceMolayUnixLinux/Exercises/2/Head/Debug/main1 
  warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
  warning: Could not load shared library symbols for linux-vdso.so.1.
  Do you need "set solib-search-path" or "set sysroot"?
  (gdb) n
  (gdb) n
  (gdb) s
  Python Exception &lt;type 'exceptions.IndexError'&gt; list index out of range: 
  (gdb) Python Exception &lt;type 'exceptions.IndexError'&gt; list index out of range: 
  q
  
  Debugger finished
  </code></pre>
  
  <p>How can I prevent this exception? Why have the exception thrown?</p>