title: Std Map giving Segmentation fault
body: <p>I am using map in a program to convert a sparse matrix into a compact array.</p>
  
  <pre><code>map&lt;int, int&gt;  m_mCustIds;
  map&lt;int, int&gt;::iterator itr;
  
   for (i=0; i&lt;m_nRatingCount; i++)
  {   
  Data* rating = m_aRatings + i;
  itr = m_mCustIds.find(rating-&gt;CustId); 
  if (itr == m_mCustIds.end())
      {
          cid = 1 + (int)m_mCustIds.size();
      }
      else
      {
          cid = itr-&gt;second;
      }
  // using cid in other data structures
  }
  </code></pre>
  
  <p>After processing a fixed number (100498277) of entries it gives me a segmentation fault.</p>
  
  <pre><code>GDB Output
  Program received signal SIGSEGV, Segmentation fault.
  0x0000000000401ea0 in std::less&lt;int&gt;::operator() (this=0x2aab0adf5ed8, __x=@0x22, __y=@0x2aab0a80f0a4)
      at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227
  227           { return __x &lt; __y; }
  </code></pre>
  
  <p>bt shows</p>
  
  <pre><code>#0  0x0000000000401ea0 in std::less&lt;int&gt;::operator() (this=0x2aab0adf5ed8, __x=@0x22, __y=@0x2aab0a80f0a4)
      at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227
  #1  0x000000000040218f in std::_Rb_tree&lt;int, std::pair&lt;int const, int&gt;, std::_Select1st&lt;std::pair&lt;int const, int&gt; &gt;, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, int&gt; &gt; &gt;::find (this=0x2aab0adf5ed8, __k=@0x2aab0a80f0a4) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:1317
  #2  0x0000000000402243 in std::map&lt;int, int, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, int&gt; &gt; &gt;::find (this=0x2aab0adf5ed8, __x=@0x2aab0a80f0a4)
      at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:534
  #3  0x0000000000401b18 in Engine::CalcMetrics (this=0x2aaaaaad9010) at At.cpp:204
  #4  0x0000000000401da3 in main () at At.cpp:141
  </code></pre>
  
  <p>Valgrind output</p>
  
  <pre><code>==18544== Invalid read of size 4
  ==18544==    at 0x401EA0: std::less&lt;int&gt;::operator()(int const&amp;, int const&amp;) const (stl_function.h:227)
  ==18544==    by 0x40218E: std::_Rb_tree&lt;int, std::pair&lt;int const, int&gt;, std::_Select1st&lt;std::pair&lt;int const, int&gt; &gt;, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, int&gt; &gt; &gt;::find(int const&amp;) (stl_tree.h:1317)
  ==18544==    by 0x402242: std::map&lt;int, int, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, int&gt; &gt; &gt;::find(int const&amp;) (stl_map.h:534)
  ==18544==    by 0x401B17: Engine::CalcMetrics() (At.cpp:204)
  ==18544==    by 0x401DA2: main (At.cpp:141)
  ==18544==  Address 0x22 is not stack'd, malloc'd or (recently) free'd
  </code></pre>
  
  <p>Valgrind also reports possilbe loss of data in other functions of map
  The program uses only 10 % of the available system memory.</p>
  
  <p>Please help</p>