title: Segmentation fault 11 in a Practice
body: <p>I'm working on an exercise for my Algorithms class and I'm getting a <code>segmentation fault:11</code> in <code>line 58</code> and I don't know why, I mean, I know that the first call to function "localiza" works fine but the second one,</p>
  
  <pre><code>*i2 = localiza(v2, 0, v2.size())*
  </code></pre>
  
  <p>provokes the segmentation fault and I don't know why.
  This algorithm is used for find an index such that <code>v[i] = i</code>. In <code>main()</code>, if <code>i1</code> and <code>i2</code> are <code>!= 0</code> means that <code>v1[i1] = i1</code> and <code>v2[i2] = i2</code>.
  I hope you could help me with the exercise. Lot of thanks guys!!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int localiza(vector &lt;int&gt; v, int primero, int ultimo){
      int i = 0;
      int izq = 0, der = 0;
  
      if(primero == ultimo)
          if (v[primero] == primero)
              return primero;
          else 
              return 0; // No existe el indice buscado
      else{
          i = (primero + ultimo) / 2; // Division entera
  
          if(v[i] == i)
              return i;
          else if(v[i] &gt; i){
              izq = localiza(v, primero, i-1); // Primera mitad
  
              if(v[i] &lt;= ultimo)
                  der = localiza(v, v[i], ultimo);
              else
                  der = 0;
  
              if(izq != 0)
                  return izq;
              else if(der != 0)
                  return der;
              else 
                  return 0;
          }
          else{
              der = localiza(v, i+1, ultimo);
  
              if(primero &lt;= v[i])
                  izq = localiza(v, primero, v[i]);
              else 
                  izq = 0;
  
              if(der != 0)
                  return der;
              else if(izq != 0)
                  return izq;
              else
                  return 0;
          }
      }
  }
  
  int main(){
      vector &lt;int&gt; v1 = {1, 2, 4, 5, 7, 9, 11};
      vector &lt;int&gt; v2 = {1, 2, 3, 4, 5, 6, 7, 7, 15, 16};
  
      int i1 = localiza(v1, 0, v1.size());
      int i2 = localiza(v2, 0, v2.size());
  
      if(i1 != 0)
          cout &lt;&lt; "Si existe el indice indicado para el primer vector";
      else
          cout &lt;&lt; "No existe el indice indicado para el primer vector";
  
      if(i2 != 0)
          cout &lt;&lt; "Si existe el indice indicado para el segundo vector";
      else
          cout &lt;&lt; "No existe el indice indicado para el segundo vector";
  
      return 0;
  }
  </code></pre>