Assertion failure :: malloc -
i running code in coding site , 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 not getting issue. help??
your scanf() format wrong.
scanf("%s[^\\n]%*c", grid[i]);
this saying scanf() for
1) string (%s)
2) character '['
3) character '^'
4) character '\'
5) character 'n'
6) character ']'
7) char (%*c), not store it
might want drop 's'
scanf("%[^\\n]%*c", grid[i]);
idea:
char buf[80]; fgets(buf, sizeof(buf)-1, stdin); sscanf(buf, "%[^\\n]", grid[i]);
i find safer separate input parsing.
further, have
char grid[x][y];
this appears dynamically allocation of "grid" based on size of x & y. not c (unless new feature). need ask, compiler using?
Comments
Post a Comment