title: Runtime error: reference binding to null pointer of type 'int' (stl_vector.h) while dealing with leetcode test cases
body: <blockquote>
  <p>SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
  /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9</p>
  </blockquote>
  <p>was the remainder of the error message.</p>
  <p>I have been working to solve the Merge 2 sorted arrays problem (<a href="https://leetcode.com/explore/learn/card/fun-with-arrays/525/inserting-items-into-an-array/3253/" rel="nofollow noreferrer">https://leetcode.com/explore/learn/card/fun-with-arrays/525/inserting-items-into-an-array/3253/</a>) for the past hour, using the vector insert function as below:</p>
  <pre><code>class Solution {
  public:
      void merge(vector&lt;int&gt;&amp; nums1, int m, vector&lt;int&gt;&amp; nums2, int n) {
          int j=0;
          if(nums1.empty() || nums1.size()==1){
              nums1.push_back(nums2[0]);
              return;
          }
          if(nums2.empty()){
              return;
          }
          
          
          for(int i=0; i&lt;(m+n); i++){
              if(j&lt;n){
                  if(nums1[i]&lt;nums2[j] &amp;&amp; nums1[i+1] &gt;= nums2[j]){
                      nums1.insert(nums1.begin()+i+1, nums2[j]);
                      j++;
                  }
                  if(nums1[i]&lt;nums2[j] &amp;&amp; nums1[i+1]==0){
                      nums1.insert(nums1.begin()+i+1, nums2[j]);
                      j++;
                  }
              }
            cout &lt;&lt; j;
          }
          int size = nums1.size()-1;
          if(nums1.size()&gt;1){
          int resiz = m+n;
          nums1.resize(resiz);
        }
          return;
      }
  };
  </code></pre>
  <p>I have got to the right solution using this. I have to resize at the end to get rid of the trailing 0s since the insert() method increases the vector size each time.</p>
  <p>The problem lies in the test case</p>
  <p>[1]         // contents of array 1</p>
  <p>1           // size of vct 1</p>
  <p>[]          // contents of array 2</p>
  <p>0           // size of vct 2</p>
  <p>Of course there may be better ways to solve the problem (and I'd appreciate those suggestions), but I'd like to understand what's causing this, since I have explicitly tried to deal with these edge cases in code.</p>