title: Runtime error while using vectors in c++ with large number of inputs
body: <p>I was trying to solve a hackerearth problems to understand c++ better.
  The Task is as follows :<br>
  The first line will consists of one integer T denoting the number of test cases.
  For each test case:</p>
  <ol>
  <li>The first line consists of two integers N and K, N being the number of elements in the array and K denotes the number of steps of rotation.</li>
  <li>The next line consists of N space separated integers , denoting the elements of the array A.</li>
  </ol>
  <p>So,If the input is like</p>
  <pre><code>1
  5 2
  1 2 3 4 5
  </code></pre>
  <p>The output should be</p>
  <pre><code>4 5 1 2 3
  </code></pre>
  <p>The code that i have written is as follows :</p>
  <pre><code>
  #include &lt;iostream&gt;
  #include&lt;bits/stdc++.h&gt;
  
  using namespace std;
  
  
  void ans(int r,vector&lt;int&gt; &amp;a)
  {
      reverse(a.begin(),a.end());
      reverse(a.begin(),a.begin()+r);
      reverse(a.begin()+r,a.end());
  }
  
  
  int main() {
      int testno;
      cin&gt;&gt;testno;
      int size,rotation,temp;
      vector&lt;vector&lt;int&gt;&gt; arr(testno);
      for(int i=0;i&lt;testno;i++)
      {
          cin&gt;&gt;size;
          cin&gt;&gt;rotation;
          for(int j=0;j&lt;size;j++)
          {
              cin&gt;&gt;temp;
              arr[i].push_back(temp);
          };
          ans(rotation,arr[i]);
      };
      for(int i=0;i&lt;arr.size();i++)
      {
          for(int j=0;j&lt;arr[i].size();j++)
              cout&lt;&lt;arr[i][j]&lt;&lt;&quot; &quot;;
          cout&lt;&lt;&quot;\n&quot;;
      };
  }
  </code></pre>
  <p>This code works fine for small test-cases or numbers.<br>
  But when the input is huge,I get the following error : <br></p>
  <pre><code>Execution failed.
  
  Stack Trace:
  *** Error in `/hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out': malloc(): memory corruption (fast): 0x0000000001b0ad50 ***
  ======= Backtrace: =========
  /lib/x86_64-linux-gnu/libc.so.6(+0x777f5)[0x7f10c1df57f5]
  /lib/x86_64-linux-gnu/libc.so.6(+0x82679)[0x7f10c1e00679]
  /lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7f10c1e021d4]
  /usr/lib/x86_64-linux-gnu/libstdc++.so.6(_Znwm+0x15)[0x7f10c2405c75]
  /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400f62]
  /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400bbb]
  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f10c1d9e840]
  /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out[0x400d49]
  ======= Memory map: ========
  00400000-00402000 r-xp 00000000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
  00601000-00602000 r--p 00001000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
  00602000-00603000 rw-p 00002000 07:00 28 /hackerearth/CPP17_a870_37de_35ba_2748/s_3a8f_d922_ff44_e648.cpp.out
  01af8000-01b2a000 rw-p 00000000 00:00 0 [heap]
  7f10bc000000-7f10bc021000 rw-p 00000000 00:00 0
  7f10bc021000-7f10c0000000 ---p 00000000 00:00 0
  7f10c1a75000-7f10c1b7d000 r-xp 00000000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
  7f10c1b7d000-7f10c1d7c000 ---p 00108000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
  7f10c1d7c000-7f10c1d7d000 r--p 00107000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
  7f10c1d7d000-7f10c1d7e000 rw-p 00108000 ca:01 48154 /lib/x86_64-linux-gnu/libm-2.23.so
  7f10c1d7e000-7f10c1f3e000 r-xp 00000000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
  7f10c1f3e000-7f10c213e000 ---p 001c0000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
  7f10c213e000-7f10c2142000 r--p 001c0000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
  7f10c2142000-7f10c2144000 rw-p 001c4000 ca:01 48130 /lib/x86_64-linux-gnu/libc-2.23.so
  7f10c2144000-7f10c2148000 rw-p 00000000 00:00 0
  7f10c2148000-7f10c215f000 r-xp 00000000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f10c215f000-7f10c235e000 ---p 00017000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f10c235e000-7f10c235f000 r--p 00016000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f10c235f000-7f10c2360000 rw-p 00017000 ca:01 18214 /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f10c2360000-7f10c2533000 r-xp 00000000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
  7f10c2533000-7f10c2732000 ---p 001d3000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
  7f10c2732000-7f10c273d000 r--p 001d2000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
  7f10c273d000-7f10c2740000 rw-p 001dd000 ca:01 16581 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
  7f10c2740000-7f10c2743000 rw-p 00000000 00:00 0
  7f10c2743000-7f10c2769000 r-xp 00000000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
  7f10c2948000-7f10c294e000 rw-p 00000000 00:00 0
  7f10c2967000-7f10c2968000 rw-p 00000000 00:00 0
  7f10c2968000-7f10c2969000 r--p 00025000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
  7f10c2969000-7f10c296a000 rw-p 00026000 ca:01 48198 /lib/x86_64-linux-gnu/ld-2.23.so
  7f10c296a000-7f10c296b000 rw-p 00000000 00:00 0
  7fff76e54000-7fff76e75000 rw-p 00000000 00:00 0 [stack]
  7fff76f09000-7fff76f0b000 r--p 00000000 00:00 0 [vvar]
  7fff76f0b000-7fff76f0d000 r-xp 00000000 00:00 0 [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
  </code></pre>
  <p>I don't understand why there is a runtime malloc error? What exactly is happening that is leading to this error for large inputs? Why is there memory corruption? Am I trying to access memory not allocated to me during the runtime? If so,Where? <br>The link to the problem,if needed is :  <a href="https://www.hackerearth.com/practice/codemonk/" rel="nofollow noreferrer">https://www.hackerearth.com/practice/codemonk/</a></p>