• Im在使用Ncurs的C类终端天球游戏上工作,我用以下代码把天球作为双倍链接清单:
typedef struct {
int y;
int x;
} coordinates;
bool coordsequal(coordinates c1, coordinates c2);
typedef struct {
coordinates coords;
char attire;
} object;
typedef object part;
/* Snake: Doubly Linked list of Parts */
typedef struct dllsnake {
part part;
struct dllsnake *prev;
struct dllsnake *next;
} snake;
由此,我确定了一项职能,将最后一项内容放在与天线相连的名单中:
snake *pop(snake *head) {
if (head == NULL) {
// List is empty, nothing to pop
return NULL;
}
snake *ptr; // line 84
for (ptr = head; ptr->next != NULL; ptr = ptr->next); // line 85
snake *new_tail = ptr->prev; // line 87
new_tail->next = NULL;
free(ptr);
return new_tail;
}
This function works as intended in the snake game, deleting the snake s tail when prompted to. However, when I run my snake game with valgrind, valgrind has many complaints about this function, including:
- Invalid read of size 8 at lines 84, 85, and 87
- Invalid free() / delete / delete[] / realloc() at line 87
- Addresses 0x4b52d10, 0x4b52d20, and 0x4b52d28 are 0 bytes inside a block of size 32 free d, line 87
更有甚者,当天花游戏时,波人完全停止工作。
我don不 地知道与这些信息做什么! 首先,我看不出我不准备阅读的记忆。 第二,如果自由声明在后面有两条线,那么为什么要看第87条? 我don不知最后一点。 任何帮助都会受到高度赞赏。