title: weird segmentation fault and valgrind analysis
body: <p>I had a code to run computer simulation of membrane. I added some minor modification which caused the weirdest segmentation fault. In my code I have set up a map:</p>
  
  <pre><code>struct index{
    int x;
    int y;
    int z;
    bool operator&lt;(const index &amp;b) const {
      bool out = true;
      if (x == b.x){
        if (y == b.y){  
      out = z &lt; b.z;
    }else out = y &lt; b.y;
      }else out = x &lt; b.x;
      return out;
    }
  };
  map&lt;index,set&lt;std::pair&lt;int, int&gt; &gt; &gt; tBoxes;
  </code></pre>
  
  <p>the segmentation fault occur when I do  </p>
  
  <pre><code>if (tBoxes.find(t) == tBoxes.end()) continue;
  </code></pre>
  
  <p>So, when I run the code through valgrind I see that:
  1. I have this when I first assign values to the map:</p>
  
  <pre><code>==26196== 6,568 (96 direct, 6,472 indirect) bytes in 1 blocks are definitely lost in loss record 86 of 145
  ==26196==    at 0x4A0666E: operator new(unsigned long) (vg_replace_malloc.c:220)
  ==26196==    by 0x4333C6: __gnu_cxx::new_allocator&lt;std::_Rb_tree_node&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::allocate(unsigned long, void const*) (new_allocator.h:88)
  ==26196==    by 0x4333EB: std::_Rb_tree&lt;membrane::index, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::_M_get_node() (stl_tree.h:358)
  ==26196==    by 0x433407: std::_Rb_tree&lt;membrane::index, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::_M_create_node(std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; const&amp;) (stl_tree.h:367)
  ==26196==    by 0x434474: std::_Rb_tree&lt;membrane::index, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; const&amp;) (stl_tree.h:819)
  ==26196==    by 0x434919: std::_Rb_tree&lt;membrane::index, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::insert_unique(std::_Rb_tree_iterator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; const&amp;) (stl_tree.h:962)
  ==26196==    by 0x434B52: std::map&lt;membrane::index, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::insert(std::_Rb_tree_iterator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt;, std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; const&amp;) (stl_map.h:420)
  ==26196==    by 0x434C3B: std::map&lt;membrane::index, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt;, std::less&lt;membrane::index&gt;, std::allocator&lt;std::pair&lt;membrane::index const, std::set&lt;std::pair&lt;int, int&gt;, std::less&lt;std::pair&lt;int, int&gt; &gt;, std::allocator&lt;std::pair&lt;int, int&gt; &gt; &gt; &gt; &gt; &gt;::operator[](membrane::index const&amp;) (stl_map.h:348)
  </code></pre>
  
  <ol>
  <li>at some later stage of the code, there is note that:</li>
  </ol>
  
  <blockquote>
    <p>Conditional jump or move depends on uninitialised value(s)</p>
  </blockquote>
  
  <ol>
  <li>There are, also, other messages by valgrind which I don't focus upon currently. At least some of them are related to these two.</li>
  </ol>
  
  <p>On a line that compares to double values which are declared previously (one is the first value generated in the simulation and the other is the element I wish to check which is determined. </p>
  
  <p>So I have two questions:
  1. About the segmentation fault. what is the meaning of this valgrind output? How can it be that setting a map value is invalid. </p>
  
  <ol>
  <li>About the conditional jump error. How can It be? Is it possible that valgrind is interpreting the error position wrong?</li>
  </ol>
  
  <p>Thank you for any help</p>
  
  <p><strong>Edit:</strong><br>
  1. <code>t</code> is an index element that is legally defined. 
  I use a loop:  </p>
  
  <pre><code>index t;
  for (int i = mini; i &lt;= maxi; i++){ //mini and maxi are previously defined
    if (i &lt; 0) t.x = i + P.boxNum; //P.boxNum is an integer, defined when I start the code
    else if (i &gt;= P.boxNum) t.x  = i - P.boxNum;
    else t.x = i;
    for (int j = minj; j &lt;= maxj; j++){//maxj and minj are previously defined
      if (j &lt; 0) t.y = j + P.boxNum;
      else if (j &gt;= P.boxNum) t.y =j - P. boxNum;
      else t.y = j;
      for(int k = mink; k &lt;= maxk; k++){//mink and maxk are previously defined
        if (k &lt; 0) t.z = k + P.zBoxNum;
        else if (k &gt;= P.boxNum) t.z =k - P. zBoxNum;
        else t.z = k;
        if (tBoxes.find(t) == eBoxes.end()) continue;
        //now I access tBoxes[t], this should be ok since if tBoxes[t] does not exist the loop should skip to the next value of k
      }
    }
  }
  </code></pre>
  
  <ol>
  <li><p>I have found the exact position of the segmentation fault by using:</p>
  
  <p>printf ("%i, %i, %i\n", t.x, t.y, t.z);
  fflush(stdout);</p></li>
  </ol>
  
  <p>both before and after the mentioned line.</p>