0xC0000005:访问冲突读取位置 - 无法读取2d结构数组中变量的内存

时间:2015-10-25 22:26:38

标签: c++ memory multidimensional-array struct

我收到以下代码的0xC0000005:访问冲突读取位置运行时错误。 Visual Studio为p *中的每个变量指定一个结构点*(x,y,z,nx,ny,nz,s,t)。我假设这是一个内存分配问题?

提前谢谢!

struct point {
    float x, y, z;      // coordinates
    float nx, ny, nz;   // surface normal
    float s, t;     // texture coords
};

void
DrawPoint(struct point *p)
{
    glNormal3f(p->nx, p->ny, p->nz);
    glTexCoord2f(p->s, p->t);
    glVertex3f(p->x, p->y, p->z);
}

void
MjbSphere(float radius, int slices, int stacks)
{
    struct point top, bot;      // top, bottom points
    struct point *p;

    // set the globals:

    NumLngs = slices;
    NumLats = stacks;

    if (NumLngs < 3)
        NumLngs = 3;

    if (NumLats < 3)
        NumLats = 3;


    // allocate the point data structure:

    Pts = new struct point*[NumLats];
    for (int i = 0; i < NumLats; i++)
        Pts[i] = new struct point[NumLngs];
    // fill the Pts structure:

    for (int ilat = 0; ilat < NumLats; ilat++)
    {
        float lat = -M_PI / 2. + M_PI * (float)ilat / (float)(NumLats - 1);
        float xz = cos(lat);
        float y = sin(lat);
        for (int ilng = 0; ilng < NumLngs; ilng++)
        {
            float lng = -M_PI + 2. * M_PI * (float)ilng / (float)(NumLngs - 1);
            float x = xz * cos(lng);
            float z = -xz * sin(lng);
            p = &Pts[ilat][ilng];
            p->x = radius * x;
            p->y = radius * y;
            p->z = radius * z;
            p->nx = x;
            p->ny = y;
            p->nz = z;
            p->s = (lng + M_PI) / (2.*M_PI);
            p->t = (lat + M_PI / 2.) / M_PI;
        }
    }

    top.x = 0.;     top.y = radius; top.z = 0.;
    top.nx = 0.;        top.ny = 1.;        top.nz = 0.;
    top.s = 0.;     top.t = 1.;

    bot.x = 0.;     bot.y = -radius;    bot.z = 0.;
    bot.nx = 0.;        bot.ny = -1.;       bot.nz = 0.;
    bot.s = 0.;     bot.t = 0.;


    // connect the north pole to the latitude NumLats-2:

    glBegin(GL_QUADS);
    for (int ilng = 0; ilng < NumLngs - 1; ilng++)
    {
        p = &Pts[NumLats - 1][ilng];
        DrawPoint(p);

        p = &Pts[NumLats - 2][ilng];
        DrawPoint(p);

        p = &Pts[NumLats - 2][ilng + 1];
        DrawPoint(p);

        p = &Pts[NumLats - 1][ilng + 1];
        DrawPoint(p);
    }
    glEnd();

    // connect the south pole to the latitude 1:

    glBegin(GL_QUADS);
    for (int ilng = 0; ilng < NumLngs - 1; ilng++)
    {
        p = &Pts[0][ilng];
        DrawPoint(p);

        p = &Pts[0][ilng + 1];
        DrawPoint(p);

        p = &Pts[1][ilng + 1];
        DrawPoint(p);

        p = &Pts[1][ilng];
        DrawPoint(p);
    }
    glEnd();


    // connect the other 4-sided polygons:

    glBegin(GL_QUADS);
    for (int ilat = 2; ilat < NumLats - 1; ilat++)
    {
        for (int ilng = 0; ilng < NumLngs - 1; ilng++)
        {
            p = Pts[(ilat - 1) * 10 + ilng];
            DrawPoint(p);

            p = Pts[(ilat - 1) * 10 + ilng + 1];
            DrawPoint(p);

            p = Pts[ilat * 10 + ilng + 1];
            DrawPoint(p);

            p = Pts[ilat * 10 + ilng];
            DrawPoint(p);
        }
    }
    glEnd();

    delete[] Pts;
    Pts = NULL;
}

0 个答案:

没有答案