0
votes

I am running code in a coding site and got following error:

solution: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. Aborted (core dumped)

Code:

#include <stdio.h>
#include <string.h>
#include <math.h>

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 && 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->x=i;
            node->y=j;
            node->prevcell=top;
            node->nextcell=NULL;
            if(top != NULL)
                top->nextcell=node;
            top=node;

            i=i-1;
        }
        //LEFT
        else if(j != 0 && 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->x=i;
            node->y=j;
            node->prevcell=top;
            node->nextcell=NULL;
            if(top != NULL)
                top->nextcell=node;
            top=node;

            j=j-1;
        }
        //RIGHT
        else if(j != y-1 && 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->x=i;
            node->y=j;
            node->prevcell=top;
            node->nextcell=NULL;
            if(top != NULL)
                top->nextcell=node;
            top=node;

            j=j+1;
        }
        //DOWN
        else if(i != x-1 && 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->x=i;
            node->y=j;
            node->prevcell=top;
            node->nextcell=NULL;
            if(top != NULL)
                top->nextcell=node;
            top=node;

            i=i+1;
        }
        else
        {
            //pop
            top=top->prevcell;
            free(top->nextcell);
            i=top->x;
            j=top->y;
        }
    }

}
/* Tail starts here */
int main() {

    int x, y;
    int pacman_x, pacman_y;
    int food_x, food_y;
    scanf( "%d %d", &pacman_x, &pacman_y);
    scanf( "%d %d", &food_x, &food_y);
    scanf( "%d %d", &x, &y);
    char grid[x][y];

    for( int i=0; i<x; i++) {
        scanf("%s[^\\n]%*c", grid[i]);
    }
    nextMove( x, y, pacman_x, pacman_y, food_x, food_y, grid);
    return 0;
}

I am not getting the issue. Could someone help??

2

2 Answers

0
votes

Your scanf() format is wrong.

scanf("%s[^\\n]%*c", grid[i]);

This is saying to scanf() for
1) a string (%s)
2) the character '['
3) the character '^'
4) the character '\'
5) the character 'n'
6) the character ']'
7) a char (%*c), but do not store it
You might want to drop the 's'

scanf("%[^\\n]%*c", grid[i]);

Idea:

char buf[80];
fgets(buf, sizeof(buf)-1, stdin);
sscanf(buf, "%[^\\n]", grid[i]);

I find it safer to separate Input from parsing.

Further, you have

char grid[x][y];

This appears to be a dynamically allocation of "grid" based on the size of x & y. This is not C (unless it a new feature). So I need to ask, what compiler are you using?

0
votes

Ohh.. I got the issue.

The following malloc call is incorrect:

       node = (cell*)malloc(sizeof(node));

It will allocate only 4 bytes (node being a pointer).

The correct version would be:

       node = (cell*)malloc(sizeof(*node));

OR

       node = (cell*)malloc(sizeof(cell));

How silly of me..!!