搜索(修改的)双向列表时的SIGSEGV

时间:2017-01-15 13:24:47

标签: c++ data-structures sigsegv

我正在编写一个控制台游戏,其拍摄机制由修改后的双向列表处理:

tank.h:

typedef
struct Shoots
{
    struct Shoot* head;
    struct Shoot* tail;
}shootsList;

typedef
struct Shoot
{
    short coords[2];
    short vect;
    std::chrono::time_point<std::chrono::system_clock> start;
    struct Shoot* next;
    struct Shoot* prev;
}shoot_t;

并在tank.cpp中有一个程序

void manageShoots(char arena[][35], char hitmap[][35])
{
    shoot_t* sht = shoots.head;
    while(sht->next != NULL) sht = sht->next; (...)

编程接收信号SIGSEGV,分段故障。 在C:... \ TS \ tank.cpp:46: while(sht-&gt; next!= NULL)sht = sht-&gt; next;

在使用manageShoots之前的main.cpp中我已经初始化了列表:

shootsList shoots;
shoots->head = NULL;
shoots->tail = NULL;

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

如果列表为空,即shoots.head == NULL,则sht-&gt; next导致SIGSEGV。 循环应如下所示:

while (sht != NULL)
{
    // do processing, if any
    sht = sht->next;
}