地下城代码中的分段错误

时间:2015-08-04 05:46:09

标签: c++ c++11 segmentation-fault

我在这段看似正常的代码中遇到了段错误 我把它缩小到这个功能:

function where_builder( $elements = array(), &$clause = array(), $index = 0)
{
    //
    $element_children = $this->return_array_where( $elements, 'parent', $elements[$index]['id'] );

    //
    $x=0;
    foreach( $element_children as $key => $value ) 
    {
        if( $x > 0 )
            array_push( $clause, ' OR ' );

        if( count( $element_children ) > 1  )
            array_push( $clause, ' ( ' );

        //
        $element_grandchildren = $this->return_array_where( $elements, 'parent', $value['id'] );

        //  
        array_push( $clause, $value['equation'] );
        //
        if( count( $element_grandchildren ) > 0 )
        {   
            array_push( $clause, ' AND ' );

            $pointer = $this->get_first_key_where( $elements, 'id', $value['id'] );

            $this->where_builder( $elements, $clause, $pointer );
        }

        if( count( $element_children ) > 1 )
            array_push( $clause, ' ) ' );

        $x++;
    }
    return $clause;
}

经过几个小时的调试,我仍然不知道它在哪里 段错似乎发生在第3或第4循环之后 roomList是 std::array<uint16_t, 5> makeExit(int rm) { std::array<uint16_t, 5> ex; std::array<uint16_t, 4> room; room = roomList[rm]; int rx, ry, rx2, ry2; while (1) { int rw = rand() % 4; switch (rw) { case 0: // north wall rx = room[2] + (rand() % room[1]); ry = room[3] - 1; rx2 = rx; ry2 = ry - 1; break; case 1: // east wall rx = room[3] + (rand() % room[0]); ry = room[2] + room[1]; rx2 = rx + 1; ry2 = ry; break; case 2: // south wall rx = room[2] + (rand() % room[1]); ry = room[3] + room[0]; rx2 = rx; ry2 = ry + 1; break; case 3: // west wall rx = room[3] + (rand() % room[0]); ry = room[2] - 1; rx2 = rx - 1; ry2 = ry; break; if (mapData[ry2][rx2] == 2) { // this is a wall, exit ex[0] = (uint16_t) rx; ex[1] = (uint16_t) ry; ex[2] = (uint16_t) rx2; ex[3] = (uint16_t) ry2; ex[4] = (uint16_t) rw; break; } } return ex; } std::vector,其中包含地牢的房间数据,mapData是std::array<uint16_t, 4>的{​​{1}},用于保存图块数据。在valgrind,它说 std::vectoruint16_t
你能帮我找到段错吗?

1 个答案:

答案 0 :(得分:0)

哇史诗般的失败:/
看起来mapData[ry2][rx2]应该是mapData[ry][rx]。 (捂脸)
我还修复了switch语句(另一个史诗般的失败:|)
我用来生成房间的函数(makeRoom())返回了一些我不想要的数字(rand() % 8 + 5而不是rand() % 8 + 3)所以我改变了它!
现在没有段错误:))