【发布时间】:2016-01-21 22:46:44
【问题描述】:
我正在使用双向链表用 C 语言编写经典的 Snake 游戏,并编写了一个函数来创建指针,为结构分配所需的空间,然后为列表中的下一个指针分配内存等等。最后函数返回指向第一个元素的指针,可以在主函数中赋值给头指针。
开始游戏时,我希望蛇的长度为 3,因此我在函数中有三个 malloc,并使用了指针、指针->下一个、指针->下一个->下一个等等,一切正常。
由于在这个过程中必须重复很多步骤,所以我想把所有这些都放入一个这样的 for 循环中:
#include <stdio.h>
#include <stdlib.h>
typedef struct snake snake;
struct snake {
int x; /* x coordinate */
int y; /* y coordinate */
snake *previous;
snake *next;
};
snake *initSnake(void) {
snake *pointer, *tmp1, *tmp2 = NULL;
/* three iterations, so the snake will have a length of three */
for( int i = 0; i<3; i++, tmp1 = tmp1->next) {
if(NULL == (tmp1 = (snake*)malloc(sizeof(snake)))) {
return NULL;
}
/* coordinates */
tmp1->x = 20;
tmp1->y = 10 + i;
/* first previous points to NULL */
tmp1->previous = tmp2;
/* temporarily store last pointer to be used for next previous pointer */
tmp2 = tmp1;
if(0 == i) {
/* store first pointer so it can be returned */
pointer = tmp1;
}
}
/* the last next pointer has to point to NULL */
tmp1 = NULL;
/* now return the pointer to the first element in list */
return pointer;
}
int main() {
/* pointer to first element in list */
snake *head = NULL;
if(NULL == (head = initSnake() ) ) {
fprintf(stderr, "Not enough memory!\n");
return EXIT_FAILURE;
}
/* here everything works fine */
printf("%d\n", head->y);
printf("%d\n", head->previous);
/* when trying to acces the content of the next element, the program crashes... */
printf("%d\n", head->next->x);
/* pause */
getchar();
}
问题是当我尝试访问主函数内列表的第二个元素时,游戏崩溃了。我怀疑有什么问题
tmp1 = tmp1->next 在 for 循环中,我并没有真正访问下一个指针,但我不完全确定。
你能帮帮我吗?
【问题讨论】:
-
可能设置 tmp1->next = NULL 会有所帮助,或者调用 calloc 而不是 malloc。并且不要强制转换 malloc 的返回值。
-
@bruceg 为什么不强制返回 malloc?我很少看到它,但我正在做 C 讲座的教授坚持要这样做。
标签: c for-loop linked-list crash initialization