title: Getting a free(): invalid pointer error
body: <p>I was trying to make a program that replaces characters e  with * on a string that has the most e's,but I'm getting a random free(): invalid pointer error whenever I run this program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  int main()
  {
      const char char1 = 'e', char2 = '*';
      int N, mostchars = 0, its;
  
      cout &lt;&lt; "N = ";
      cin &gt;&gt; N;
      string strings[N];
  
      for(int i = 1; i &lt;= N; i++)
      {
          cin &gt;&gt; strings[i];
          if(mostchars &lt; count(strings[i].begin(), strings[i].end(), char1))
          {
              mostchars = count(strings[i].begin(), strings[i].end(), char1);
              its = i;
          }
      }
      replace(strings[its].begin(), strings[its].end(), char1, char2);
      cout &lt;&lt; strings[its] &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>And here's the gdb output:</p>
  
  <pre><code>*** Error in `/home/hiddendirishidden/STRING/bin/Debug/STRING': free(): invalid pointer: 0x00007fffffffe6a0 ***
  ======= Backtrace: =========
  /usr/lib/libc.so.6(+0x6f364)[0x7ffff720a364]
  /usr/lib/libc.so.6(+0x74d96)[0x7ffff720fd96]
  /usr/lib/libc.so.6(+0x7557e)[0x7ffff721057e]
  /usr/lib/libstdc++.so.6(_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_mutateEmmPKcm+0xfd)[0x7ffff7b765dd]
  /usr/lib/libstdc++.so.6(_ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE+0x314)[0x7ffff7afd624]
  /home/hiddendirishidden/STRING/bin/Debug/STRING[0x400e13]
  /usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7ffff71bb710]
  /home/hiddendirishidden/STRING/bin/Debug/STRING[0x400c39]
  ======= Memory map: ========
  00400000-00402000 r-xp 00000000 08:21 1452363                            /home/hiddendirishidden/STRING/bin/Debug/STRING
  00601000-00602000 rw-p 00001000 08:21 1452363                            /home/hiddendirishidden/STRING/bin/Debug/STRING
  00602000-00634000 rw-p 00000000 00:00 0                                  [heap]
  7ffff0000000-7ffff0021000 rw-p 00000000 00:00 0 
  7ffff0021000-7ffff4000000 ---p 00000000 00:00 0 
  7ffff719b000-7ffff7333000 r-xp 00000000 08:21 396432                     /usr/lib/libc-2.23.so
  7ffff7333000-7ffff7532000 ---p 00198000 08:21 396432                     /usr/lib/libc-2.23.so
  7ffff7532000-7ffff7536000 r--p 00197000 08:21 396432                     /usr/lib/libc-2.23.so
  7ffff7536000-7ffff7538000 rw-p 0019b000 08:21 396432                     /usr/lib/libc-2.23.so
  7ffff7538000-7ffff753c000 rw-p 00000000 00:00 0 
  7ffff753c000-7ffff7552000 r-xp 00000000 08:21 396763                     /usr/lib/libgcc_s.so.1
  7ffff7552000-7ffff7751000 ---p 00016000 08:21 396763                     /usr/lib/libgcc_s.so.1
  7ffff7751000-7ffff7752000 rw-p 00015000 08:21 396763                     /usr/lib/libgcc_s.so.1
  7ffff7752000-7ffff7855000 r-xp 00000000 08:21 396490                     /usr/lib/libm-2.23.so
  7ffff7855000-7ffff7a55000 ---p 00103000 08:21 396490                     /usr/lib/libm-2.23.so
  7ffff7a55000-7ffff7a56000 r--p 00103000 08:21 396490                     /usr/lib/libm-2.23.so
  7ffff7a56000-7ffff7a57000 rw-p 00104000 08:21 396490                     /usr/lib/libm-2.23.so
  7ffff7a57000-7ffff7bc9000 r-xp 00000000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
  7ffff7bc9000-7ffff7dc9000 ---p 00172000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
  7ffff7dc9000-7ffff7dd3000 r--p 00172000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
  7ffff7dd3000-7ffff7dd5000 rw-p 0017c000 08:21 396798                     /usr/lib/libstdc++.so.6.0.21
  7ffff7dd5000-7ffff7dd9000 rw-p 00000000 00:00 0 
  7ffff7dd9000-7ffff7dfc000 r-xp 00000000 08:21 396431                     /usr/lib/ld-2.23.so
  7ffff7fc5000-7ffff7fcb000 rw-p 00000000 00:00 0 
  7ffff7ff7000-7ffff7ff8000 rw-p 00000000 00:00 0 
  7ffff7ff8000-7ffff7ffa000 r--p 00000000 00:00 0                          [vvar]
  7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
  7ffff7ffc000-7ffff7ffd000 r--p 00023000 08:21 396431                     /usr/lib/ld-2.23.so
  7ffff7ffd000-7ffff7ffe000 rw-p 00024000 08:21 396431                     /usr/lib/ld-2.23.so
  7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 
  7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  
  Program received signal SIGABRT, Aborted.
  0x00007ffff71ce2a8 in raise () from /usr/lib/libc.so.6
  </code></pre>
  
  <p>I'm not sure why that happens. Maybe I did something wrong, after all this is the first time I'm trying out the algorithm library.</p>