title: Unexplainable malloc.c:2401: sysmalloc: Assertion Error
body: <p>I challenge you to find what the heck is causing my program to break, I've been trying to find a reasoning behind this but I haven't been able to find even the slightest bit.</p>
  <p>My program is supposed to read values from a file and depending on what it reads create 2 matrix: first value on the file is rows and second one cols. (Then it's supposed to do some algorithms but that works just fine, it's failing before that)</p>
  <p>So here's my problem: I keep getting the following error message when running the program <em><strong>only</strong></em> when the rows value is 30. I haven't been able to find a single value that makes this fail other than 30. I want to fix this but I can't find what's causing the program to fail to begin with, I don't know what else to try. I debugged it and the program fails when creating the first matrix after reading all the info from the file.</p>
  <pre><code>    potter: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) &gt;= MINSIZE &amp;&amp; prev_inuse (old_top) &amp;&amp; ((unsigned long) old_end &amp; (pagesize - 1)) == 0)' failed.
  Aborted (core dumped)
  </code></pre>
  <p>The funniest thing is the program was working just fine before adding the last function. I must have changed something without noticing and broken the program (I don't have any older versions and removing the function doesn't help)</p>
  <p>Here's a link to the hole code and test files if anyone is interested: <a href="https://drive.google.com/open?id=1tcKXainj021fwjAAGN3cNdsU50p2Neou" rel="nofollow noreferrer">https://drive.google.com/open?id=1tcKXainj021fwjAAGN3cNdsU50p2Neou</a></p>
  <p>Edit: The program fails in the following part of the code</p>
  <pre><code>M=new int * [n+1]                  
          if(M){                             
                  for(int i=0; i&lt;=n;i++){       
                          M[i]=new int [T+1];   
                          if(!M[i]){       
                                  cerr&lt;&lt;&quot;Error&quot;;
                                  exit(-1);
                          }
                  }
                  for(int i=0;i&lt;n+1;i++)
                          for(int j=0;j&lt;T+1;j++)
                                  M[i][j]=-1;
          }else{
                  cerr&lt;&lt;&quot;Error&quot;;
                  exit(-1);
          }
  </code></pre>
  <p>Concretely when trying to do M[i]=new int [T+1]; when i=10 <strong>only</strong> if n=30</p>
  <hr />
  <h1>Solved</h1>
  <p>The problem was a loop going outside of allocated memory beforehand corrupting it and causing the program to fail later.</p>
  <pre><code> decisions=new int [n];
                  for(int i=0;i&lt;=n;i++)
                          decisions[i]=0;
  </code></pre>
  <p>Honestly I don't know how the program didn't fail more having done something like this. (This was easily solved by changing n to n+1 in decisions=new int [n+1];)</p>