title: Empty vector characterstics in C++
body: <p>The following function in the program searches for a target value in matrix and returns true if it is present else false and it gave error when a vector - <code>([])</code> or <code>([[]])</code> is passed i.e. whenever we passed an empty vector.</p>
  <pre><code>bool searchMatrix(vector&lt;vector&lt;int&gt;&gt;&amp; matrix, int target) {
          int i = 0;        
          int m = matrix[0].size();        
          int n = matrix.size();        
          int j = m - 1;        
          if(n == 0 || m == 0)
              return false;
          if(target &gt; matrix[n - 1][m - 1] || target &lt; matrix[0][0])
              return false;
          while(j &gt;= 0 &amp;&amp; i &lt; n){
              if(matrix[i][j] == target)
                  return true;
              if(matrix[i][j] &gt; target)
                  j--;
              else
                  i++;                        
              }
          return false;
      } 
  </code></pre>
  <p>The error shown is Line 1038: Char 9: runtime error: reference binding to null pointer of type 'std::vector&lt;int, std::allocator&gt;' (stl_vector.h)
  SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:1043:9</p>
  <p>My error got rectified when I added initial line in my function as -</p>
  <pre><code>if(matrix.size() == 0 || matrix[0].size() == 0)
  return false;
  </code></pre>
  <p>I didn't get why the first code gave an error if i can compare matrix.size() and matrix[0].size() with zero why can't I first store it in variable m and n and then compare it with zero.</p>