# Random decleration problem

This topic is 5411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have no idea why this is happening. What happens is, when i declare CVector3 PolyMap[1023*1023]; It makes the whole program have a segmentation fault. Even when i commented out everything in the function. It still gives me an error. Here is the code for the CalculateNormalMap() function:
void CIrisTerrain::CalculateNormalMap()
{
int X, Y;
int x,y,z;
CVector3 normal,v1,v2,v3,v4;
CVector3 pos[3];
CVector3 PolyMap[1023*1023];
//BLOODY THING WONT LET ME DO BIGGER THAN 416, BUGGER!
//OK, NOW IT DOSNT WORK AT ALL!

//Find values for polymap
for(X=0;X<=1023;X++)
{
for(Y=0;Y<=1023;Y++)
{
//Vertex [0]
x=X;
z=Y;
y=Height(X,Y);
pos[0].x=x;
pos[0].y=y;
pos[0].z=z;

//Vertex [1]
x=X;
z=Y+1;
y=Height(X,Y+1);
pos[1].x=x;
pos[1].y=y;
pos[1].z=z;

//Vertex [2]
x=X+1;
z=Y;
y=Height(X+1,Y);
pos[2].x=x;
pos[2].y=y;
pos[2].z=z;

PolyMap[X+(Y*1023)] = Normal(pos);
}
}

//Find values for NormalMap based on polyMap
for(X=1;X<=1024;X++)
{
for(Y=1;Y<=1024;Y++)
{
v1.x = PolyMap[(X-1)+((Y-1)*1023)].x;
v1.y = PolyMap[(X-1)+((Y-1)*1023)].y;
v1.z = PolyMap[(X-1)+((Y-1)*1023)].z;

v2.x = PolyMap[(X)+((Y-1)*1023)].x;
v2.y = PolyMap[(X)+((Y-1)*1023)].y;
v2.z = PolyMap[(X)+((Y-1)*1023)].z;

v3.x = PolyMap[(X)+((Y)*1023)].x;
v3.y = PolyMap[(X)+((Y)*1023)].y;
v3.z = PolyMap[(X)+((Y)*1023)].z;

v4.x = PolyMap[(X-1)+((Y)*1023)].x;
v4.y = PolyMap[(X-1)+((Y)*1023)].y;
v4.z = PolyMap[(X-1)+((Y)*1023)].z;

normal = v1+v2+v3+v4;
normal = Normalize(normal);
NormalMap[X+(Y*1024)] = normal;

}
}
}

It gives me no errors compiling.
Help!



##### Share on other sites
You can't declare something that big on the stack, you need to declare it on the heap by using new.

CVector3* PolyMap = new CVector3[1023*1023];

##### Share on other sites
Ok, iadded that in. But im afraid it still refuses to work. Here is the new code:
void CIrisTerrain::CalculateNormalMap(){    int X, Y;    int x,y,z;    CVector3 normal,v1,v2,v3,v4;    CVector3 pos[3];    CVector3* PolyMap = NULL;    PolyMap = new CVector3[1023*1023];    //DEBBUGGER JUST FREEZES ON THIS BIT. IT JUST SAYS THAT IT  HAS A VALUE OF 0x00            //Find values for polymap    for(X=0;X<=1023;X++)    {        for(Y=0;Y<=1023;Y++)        {            //Vertex [0]            x=X;            z=Y;            y=Height(X,Y);             pos[0].x=x;            pos[0].y=y;            pos[0].z=z;                        //Vertex [1]            x=X;            z=Y+1;            y=Height(X,Y+1);             pos[1].x=x;            pos[1].y=y;            pos[1].z=z;                        //Vertex [2]            x=X+1;            z=Y;            y=Height(X+1,Y);             pos[2].x=x;            pos[2].y=y;            pos[2].z=z;                        PolyMap[X+(Y*1023)] = Normal(pos);        }    }        //Find values for NormalMap based on polyMap    for(X=1;X<=1024;X++)    {        for(Y=1;Y<=1024;Y++)        {            v1 = PolyMap[(X-1)+((Y-1)*1023)];            //v1.y = PolyMap[(X-1)+((Y-1)*1023)]->y;            //v1.z = PolyMap[(X-1)+((Y-1)*1023)]->z;                        v2 = PolyMap[(X)+((Y-1)*1023)];            //v2.y = PolyMap[(X)+((Y-1)*1023)]->y;           // v2.z = PolyMap[(X)+((Y-1)*1023)]->z;            v3 = PolyMap[(X)+((Y)*1023)];           // v3.y = PolyMap[(X)+((Y)*1023)]->y;           // v3.z = PolyMap[(X)+((Y)*1023)]->z;                        v4 = PolyMap[(X-1)+((Y)*1023)];           // v4.y = PolyMap[(X-1)+((Y)*1023)]->y;           // v4.z = PolyMap[(X-1)+((Y)*1023)]->z;                        normal = v1+v2+v3+v4;            normal = Normalize(normal);            NormalMap[X+(Y*1024)] = normal;                    }    }        delete[] PolyMap;}

I think its just not declaring it right. plz help!

##### Share on other sites
Quote:
 Original post by Kris2456...CVector3 PolyMap[1023*1023];...for(X=0;X<=1023;X++)for(Y=0;Y<=1023;Y++)...PolyMap[X+(Y*1023)] = Normal(pos);...

Your map is 1023x1023, so X goes from 0 to 1022 and Y too.

But you let X go from 0 to 1023 (and Y too) and then access PolyMap[1023+1023*1023] which is not part of the array.

##### Share on other sites
Thx for that, but unfortunatly, even with the loops commented. The thing still tells me its wrong. As i said, the debugger just stops going to the next step when it says:
PolyMap = new CVector3[1023*1023];

##### Share on other sites
Whats also strange is, that when i try and acces elements of the array, it tells me this:

41 C:\Documents and Settings\Kris2456\My Documents\Programming\Engine\Terrain.cpp
base operand of ->' has non-pointer type CVector3'

I got this from typing in: PolyMap[0]->x;

This is really getting on my nerves

##### Share on other sites
Quote:
 Original post by Kris2456Whats also strange is, that when i try and acces elements of the array, it tells me this:41 C:\Documents and Settings\Kris2456\My Documents\Programming\Engine\Terrain.cppbase operand of ->' has non-pointer type CVector3'I got this from typing in: PolyMap[0]->x;This is really getting on my nerves

PolyMap is a pointer to an array of CVector3's. When you index the array you aren't getting a pointer to that element, you're accessing that element itself.

##### Share on other sites
Quote:
 Original post by MikeMJHPolyMap is a pointer to an array of CVector3's. When you index the array you aren't getting a pointer to that element, you're accessing that element itself.

Sorry, i didnt quite understand you there. If you could explain it more slowly. Heres a def of CVector3 if it helps:
typedef struct CVector3{    CVector3(){}        CVector3(float X, float Y, float Z)    {        x=X;y=Y;z=Z;    }        //Operators    CVector3 operator+(CVector3 vector)    {        return CVector3(vector.x+x, vector.y+y, vector.z+z);    }        CVector3 operator-(CVector3 vector)    {        return CVector3(x - vector.x, y - vector.y, z - vector.z);    }        CVector3 operator*(float num)    {        return CVector3(x*num, y*num, z*num);    }        CVector3 operator/(float num)    {        return CVector3(x/num, y/num, z/num);    }        float x, y, z;};

##### Share on other sites
when you have a pointer and you use the '->' operator the pointer
becomes dereferenced [new window]. when you
access and index of a pointer you automaticaly dereference that
pointer.

eg.
struct A {    int zz;};A *p = new A[5];(p+3)->zz = 5;// is the same as...p[3].zz = 5;delete [] p;

##### Share on other sites
Thx for all the help, i fixed it now. I was just going to ask, does anyone think this methos is any good. I mean, my terrain isnt that smooth.

• 21
• 13
• 9
• 17
• 13