title: Assertion failure :: malloc
body: <p>I am running code in a coding site and got following error:</p>
  
  <p>solution: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &amp;((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)->size &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed.
  Aborted (core dumped)</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  
  typedef struct cell
  {
      int x;
      int y;
      struct cell *prevcell;
      struct cell *nextcell;
  }cell;
  
  
  /* Head ends here */
  void nextMove(int x, int y, int pacman_x, int pacman_y, int food_x, int food_y, char grid[x][y]){
      //logic here    
      int i=pacman_x;
      int j=pacman_y;
  
  
      cell *top,*node;
      top = NULL;
  
      while(grid[i][j] != '.')
      {
          node = NULL;
          //UP
          if(i != 0 &amp;&amp; grid[i-1][j] != '%')
          {
              if(grid[i][j] != 'd')
              {
                  printf("%d %d\n",i,j);
                  grid[i][j]='d';
              }
              //push
              node = (cell*)malloc(sizeof(node));
              node-&gt;x=i;
              node-&gt;y=j;
              node-&gt;prevcell=top;
              node-&gt;nextcell=NULL;
              if(top != NULL)
                  top-&gt;nextcell=node;
              top=node;
  
              i=i-1;
          }
          //LEFT
          else if(j != 0 &amp;&amp; grid[i][j-1] != '%')
          {
              if(grid[i][j] != 'd')
              {
                  printf("%d %d\n",i,j);
                  grid[i][j]='d';
              }
              //push
              node = (cell*)malloc(sizeof(node));
              node-&gt;x=i;
              node-&gt;y=j;
              node-&gt;prevcell=top;
              node-&gt;nextcell=NULL;
              if(top != NULL)
                  top-&gt;nextcell=node;
              top=node;
  
              j=j-1;
          }
          //RIGHT
          else if(j != y-1 &amp;&amp; grid[i][j+1] != '%')
          {
              if(grid[i][j] != 'd')
              {
                  printf("%d %d\n",i,j);
                  grid[i][j]='d';
              }
              //push
              node = (cell*)malloc(sizeof(node));
              node-&gt;x=i;
              node-&gt;y=j;
              node-&gt;prevcell=top;
              node-&gt;nextcell=NULL;
              if(top != NULL)
                  top-&gt;nextcell=node;
              top=node;
  
              j=j+1;
          }
          //DOWN
          else if(i != x-1 &amp;&amp; grid[i+1][j] != '%')
          {
              if(grid[i][j] != 'd')
              {
                  printf("%d %d\n",i,j);
                  grid[i][j]='d';
              }
              //push
              node = (cell*)malloc(sizeof(node));
              node-&gt;x=i;
              node-&gt;y=j;
              node-&gt;prevcell=top;
              node-&gt;nextcell=NULL;
              if(top != NULL)
                  top-&gt;nextcell=node;
              top=node;
  
              i=i+1;
          }
          else
          {
              //pop
              top=top-&gt;prevcell;
              free(top-&gt;nextcell);
              i=top-&gt;x;
              j=top-&gt;y;
          }
      }
  
  }
  /* Tail starts here */
  int main() {
  
      int x, y;
      int pacman_x, pacman_y;
      int food_x, food_y;
      scanf( "%d %d", &amp;pacman_x, &amp;pacman_y);
      scanf( "%d %d", &amp;food_x, &amp;food_y);
      scanf( "%d %d", &amp;x, &amp;y);
      char grid[x][y];
  
      for( int i=0; i&lt;x; i++) {
          scanf("%s[^\\n]%*c", grid[i]);
      }
      nextMove( x, y, pacman_x, pacman_y, food_x, food_y, grid);
      return 0;
  }
  </code></pre>
  
  <p>I am not getting the issue. Could someone help??</p>