0

我在编码站点中运行代码并收到以下错误:

解决方案: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)' 失败。中止(核心转储)

代码:

#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;
}

我没有得到这个问题。有人可以帮忙吗??

4

2 回答 2

0

哦..我有问题。

以下 malloc 调用不正确:

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

它将只分配 4 个字节(节点是一个指针)。

正确的版本是:

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

或者

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

我多么愚蠢..!!

于 2013-05-25T21:38:41.307 回答
0

您的 scanf() 格式错误。

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

这就是说 scanf() 用于
1) 字符串 (%s)
2) 字符 '['
3) 字符 '^'
4) 字符 '\'
5) 字符 'n'
6) 字符 '] '
7) 一个字符 (%*c),但不要存储它
您可能想要删除 's'

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

主意:

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

我发现将输入与解析分开更安全。

此外,你有

char grid[x][y];

这似乎是基于 x 和 y 大小的“网格”的动态分配。这不是 C(除非它是新功能)。所以我需要问一下,你使用的是什么编译器?

于 2013-05-24T14:18:27.807 回答