2013-05-21 48 views
0

我運行在編碼的網站代碼,並得到了以下錯誤:斷言失敗:: malloc的

解決方案:malloc.c:2369:SYSMALLOC:斷言`(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 * (爲size_t))) - 1)))& &((old_top) - >大小爲0x1 &)& &((無符號長整數)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; 
} 

我沒有得到這個問題。有人可以幫忙嗎?

回答

0

哦..我得到了問題。

以下malloc調用不正確:

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

它將僅分配4字節(節點是指針)。

正確的版本是:

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

OR

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

多麼愚蠢的我.. !!

0

您的scanf()格式不正確。

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

這話說到scanf()的用於
1)的字符串(%S)
2)的字符 '['
3)的字符 '^'
4)字符' \」
5)字符 'N'
6)字符 ']'
7)的燒焦(%* C),但不存儲它
你可能想砸的'

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(除非它是一個新功能)。所以我需要問,你在使用什麼編譯器?

+0

嗨,謝謝你的回答。問題來自Hackerrank.com。他們使用以下編譯器:gcc 4.7.3,C99模式。scanf()的格式和網格分配都可以工作。實際上,從評論「尾巴從這裏開始」的所有內容都由網站本身添加。 – user2407394