无法访问内存,分段错误

时间:2012-08-11 18:31:38

标签: stl stack segmentation-fault

我正在尝试使用模拟递归版本的迭代算法来实现Sierpinski Gasket。

我遇到了分段错误。可能该函数试图取消引用NULL指针,但我找不到它。问题应该在if块中,而不是在else中。因为它跳过if块时工作正常。谢谢你的帮助。

GLfloat points[18*(3^GENCOUNT)];

std::stack<Pyramid *> Q;

// initial Pyramid
Q.push(new Pyramid(Vec3(0.0f, 0.4f, 0.0f),
                    Vec3(-0.4f, -0.4f, 0.4f),
                    Vec3(0.4f, -0.4f, 0.4f),
                    Vec3(0.4f, -0.4f, -0.4f),
                    Vec3(-0.4f, -0.4f, -0.4f), 0));

int i = 0;
while(!Q.empty())
{
    Pyramid *topPyr = Q.top();
    Q.pop();

    if(topPyr->mGeneration < GENCOUNT)
    {
        Vec3 baseVec3 = (((topPyr->mV2+topPyr->mV3)/2.0f) + ((topPyr->mV4+topPyr->mV5)/2.0f)) / 2.0f;

        Q.push(new Pyramid(topPyr->mV1,
                           (topPyr->mV1+topPyr->mV2)/2.0f,
                           (topPyr->mV1+topPyr->mV3)/2.0f,
                           (topPyr->mV1+topPyr->mV4)/2.0f,
                           (topPyr->mV1+topPyr->mV5)/2.0f, topPyr->mGeneration+1));
        Q.push(new Pyramid((topPyr->mV1+topPyr->mV2)/2.0f,
                           topPyr->mV2,
                           (topPyr->mV2+topPyr->mV3)/2.0f,
                           baseVec3,
                           (topPyr->mV2+topPyr->mV5)/2.0f, topPyr->mGeneration+1));
        Q.push(new Pyramid((topPyr->mV1+topPyr->mV3)/2.0f,
                           (topPyr->mV2+topPyr->mV3)/2.0f,
                           topPyr->mV3,
                           (topPyr->mV3+topPyr->mV4)/2.0f,
                           baseVec3, topPyr->mGeneration+1));
        Q.push(new Pyramid((topPyr->mV1+topPyr->mV4)/2.0f,
                           baseVec3,
                           (topPyr->mV3+topPyr->mV4)/2.0f,
                           topPyr->mV4,
                           (topPyr->mV4+topPyr->mV5)/2.0f, topPyr->mGeneration+1));
        Q.push(new Pyramid((topPyr->mV1+topPyr->mV5)/2.0f,
                           (topPyr->mV2+topPyr->mV5)/2.0f,
                           baseVec3,
                           (topPyr->mV4+topPyr->mV5)/2.0f,
                           topPyr->mV5, topPyr->mGeneration+1));
    }
    else
    {
        memcpy(points+i, topPyr->mV1.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV2.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV3.mVec3, sizeof(GLfloat)*3);i+=3;

        memcpy(points+i, topPyr->mV1.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV3.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV4.mVec3, sizeof(GLfloat)*3);i+=3;

        memcpy(points+i, topPyr->mV1.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV4.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV5.mVec3, sizeof(GLfloat)*3);i+=3;

        memcpy(points+i, topPyr->mV1.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV5.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV2.mVec3, sizeof(GLfloat)*3);i+=3;

        memcpy(points+i, topPyr->mV2.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV3.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV4.mVec3, sizeof(GLfloat)*3);i+=3;

        memcpy(points+i, topPyr->mV4.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV5.mVec3, sizeof(GLfloat)*3);i+=3;
        memcpy(points+i, topPyr->mV2.mVec3, sizeof(GLfloat)*3);i+=3;
    }
    delete topPyr;
}

1 个答案:

答案 0 :(得分:0)

我的C ++非常生疏,但假设没有发生操作符重载,则

3^GENCOUNT
你的第一行中的

表示“3和GENCOUNT之间的按位异或”?我很确定你的意思是取幂,即3来取代GENCOUNT。