0

我正在尝试使用模拟递归版本的迭代算法来实现谢尔宾斯基垫片。

我遇到了分段错误。可能该函数正在尝试取消引用 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;
}
4

1 回答 1

0

我的 C++ 非常生锈,但假设没有发生运算符重载,则不会

3^GENCOUNT

在您的第一行中的意思是“3 和 GENCOUNT 之间的按位异或”?我很确定您的意思是取幂,即 GENCOUNT 的 3 次方。

于 2012-08-11T18:40:37.380 回答