title: Unable to resolve segmentation fault
body: <p>Hello  am <strong>newbie</strong> here
  am getting segmentation fault when running the program but unable to find reason behind it .
  I have googled and know that it occurs due to memory violation but unable to find what is causing seg fault in my code.</p>
  <p><strong>Code below</strong></p>
  <pre><code>#include &lt;iostream&gt;
  #include&lt;algorithm&gt;
  using namespace std;
  int main()
  {
   int t, n, m;
      int  arr[n];
      cin &gt;&gt; t;
      while (t--)
      {
          cin &gt;&gt; n &gt;&gt; m;
          for (int i = 0; i &lt; n; i++)
          {
              cin &gt;&gt; arr[i];
          }
          sort(arr, arr + n);
          int distinct = 1;
          for (int i = 1; i &lt; n; i++)
          {
              if (arr[i] != arr[i - 1])
              {
                  distinct++;
              }
          }
          if (distinct &lt; m)
          {
              cout &lt;&lt; &quot;YES&quot; &lt;&lt; endl;
          }
          else
          {
              cout &lt;&lt; &quot;NO&quot; &lt;&lt; endl;
          }
      }
  
      return 0;
  }
  </code></pre>
  <p><strong>I used lldb for debugging and got results as follows:</strong></p>
  <pre><code>divyangbhamat@Divyangs-MacBook-Air CodeChef % lldb ./a.out
  (lldb) target create &quot;./a.out&quot;
  Current executable set to '/Users/divyangbhamat/Documents/C++/CodeChef/a.out' (x86_64).
  (lldb) run
  Process 1316 launched: '/Users/divyangbhamat/Documents/C++/CodeChef/a.out' (x86_64)
  1
  5
  6
  1
  2
  1
  5
  3
  Process 1316 stopped
  * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7ffe00000007)
      frame #0: 0x00007fff2029b328 libc++.1.dylib`unsigned int std::__1::__sort3&lt;std::__1::__less&lt;int, int&gt;&amp;, int*&gt;(int*, int*, int*, std::__1::__less&lt;int, int&gt;&amp;) + 4
  libc++.1.dylib`std::__1::__sort3&lt;std::__1::__less&lt;int, int&gt;&amp;, int*&gt;:
  -&gt;  0x7fff2029b328 &lt;+4&gt;:  movl   (%rsi), %ecx
      0x7fff2029b32a &lt;+6&gt;:  movl   (%rdi), %r8d
      0x7fff2029b32d &lt;+9&gt;:  movl   (%rdx), %r9d
      0x7fff2029b330 &lt;+12&gt;: cmpl   %r8d, %ecx
  Target 0: (a.out) stopped.
  (lldb) bt
  * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7ffe00000007)
    * frame #0: 0x00007fff2029b328 libc++.1.dylib`unsigned int std::__1::__sort3&lt;std::__1::__less&lt;int, int&gt;&amp;, int*&gt;(int*, int*, int*, std::__1::__less&lt;int, int&gt;&amp;) + 4
      frame #1: 0x00007fff2029b3a2 libc++.1.dylib`unsigned int std::__1::__sort4&lt;std::__1::__less&lt;int, int&gt;&amp;, int*&gt;(int*, int*, int*, int*, std::__1::__less&lt;int, int&gt;&amp;) + 31
      frame #2: 0x00007fff2029b40a libc++.1.dylib`unsigned int std::__1::__sort5&lt;std::__1::__less&lt;int, int&gt;&amp;, int*&gt;(int*, int*, int*, int*, int*, std::__1::__less&lt;int, int&gt;&amp;) + 37
      frame #3: 0x0000000100003101 a.out`void std::__1::sort&lt;int*, std::__1::__less&lt;int, int&gt; &gt;(__first=0x00007ffe00000003, __last=0x00007ffe00000017, __comp=__less&lt;int, int&gt; @ 0x00007ffeefbff6f8) at algorithm:4125:5
      frame #4: 0x0000000100002ffd a.out`void std::__1::sort&lt;int*&gt;(__first=0x00007ffe00000003, __last=0x00007ffe00000017) at algorithm:4133:5
      frame #5: 0x0000000100002ef5 a.out`main at nobelPrize.cpp:16:9
      frame #6: 0x00007fff20353621 libdyld.dylib`start + 1
      frame #7: 0x00007fff20353621 libdyld.dylib`start + 1
  (lldb)
  </code></pre>
  <p>Frame <strong>#5</strong> says problem <strong>in 16.9</strong> line that is the <strong>sort function</strong> but unable to find why it is causing seg fault here</p>
  <p><strong>Thank you for your help and time.</strong></p>