title: Find segmentation fault while passing map to function C++
body: <p>I can't find the segmentation fault which is occuring on the line where I am calling <code>calculate_grundy</code> in <code>main</code>. Please help.</p>
  
  <p>I have also added a screenshot of my debugger while debugging the code.</p>
  
  <p>This is a question of a running contest so please don't answer logic problems instead of helping me in finding the segmentation fault.</p>
  
  <p>I am passing the <code>dp</code> map for storing the subproblems, as done in dynamic programming.</p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  #define ll long long int
  using namespace std;
  
  ll calculate_mex(unordered_set&lt;ll&gt; Set)
  {
      ll mex = 0;
      while(Set.find(mex)!=Set.end())
      {
          mex++;
      }
      return mex;
  }
  
  ll calculate_grundy(ll n,ll m,map&lt;pair&lt;ll,ll&gt;,ll&gt; &amp;dp)
  {
      auto it = dp.find(make_pair(n,m));
      if(it!=dp.end())
      {
          return it-&gt;second;
      }
      else
      {
          ll ans;
          ll greater = max(n,m);
          ll smaller = min(n,m);
          if(n==m || m==0 || n==0)
          {
              return 0;
          }
          else if(n==1 || m==1)
          {
              return n==1?m:n;
          }
          unordered_set&lt;ll&gt; Set;
          ll limit = greater%smaller;
          for(ll i=1;i&lt;=limit;i++)
          {
              ll mult = smaller*i;
              Set.insert(calculate_grundy(greater-mult,smaller,dp));
          }
          ans = calculate_mex(Set);
          dp.insert(make_pair(make_pair(n,m),ans));
          return ans;
      }
  }
  
  int main()
  {
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      ll t;
      cin&gt;&gt;t;
      while(t--)
      {
          ll n,m;
          cin&gt;&gt;n&gt;&gt;m;
          map&lt;pair&lt;ll,ll&gt;,ll&gt; dp;
          if(calculate_grundy(n,m,dp)==0)
          {
              cout&lt;&lt;"Ari"&lt;&lt;"\n";
          }
          else
          {
              cout&lt;&lt;"Rich"&lt;&lt;"\n";
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/0UFnh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0UFnh.png" alt="image"></a></p>