【问题标题】:SIGSEGV when searching through the (modified) bidirectional list搜索(修改后的)双向列表时的 SIGSEGV
【发布时间】:2017-01-15 13:24:47
【问题描述】:

我正在编写一个由修改后的双向列表处理的射击机制的主机游戏:

坦克.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->next != NULL) sht = sht->next;

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

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

我错过了什么吗?

【问题讨论】:

  • 我们需要您的其余代码:next 为 NULL??以及分配新单元时的 malloc 部分?

标签: c++ data-structures segmentation-fault


【解决方案1】:

如果列表为空,即shoots.head == NULL,则 sht->next 会导致 SIGSEGV。 循环应该是这样的:

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

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2019-06-23
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多