# Thread: Help with arrays and seg faults

1. ## Help with arrays and seg faults

I have a function to calculate the normals of vertices in my terrain. I already have the normals for each triangle so I am just adding all the adjacent triangles normals to get a normal for the vertice. I'm getting seg faults, I think from going out of the bounds of my vector. Here is the function

Code:
```void Map::calcVertexNorms()
{
/*It works if this get() is uncommented */
//std::cin.get();
float l;
pos normal;
int pitch = numTrisW*2;

for (int i=0; i < pitch*numTrisH; i+=2)
{
normal.x += mapTris[i].norm.x;
normal.y += mapTris[i].norm.y;
normal.z += mapTris[i].norm.z;
normal.x += mapTris[i+1].norm.x;
normal.y += mapTris[i+1].norm.y;
normal.z += mapTris[i+1].norm.z;
if (i - pitch > 0)
{
normal.x += mapTris[i-pitch].norm.x;
normal.y += mapTris[i-pitch].norm.y;
normal.z += mapTris[i-pitch].norm.z;
}
if (i - pitch > 0 && i % pitch != 509)
{
normal.x += mapTris[i-pitch-1].norm.x;
normal.y += mapTris[i-pitch-1].norm.y;
normal.z += mapTris[i-pitch-1].norm.z;
normal.x += mapTris[i-pitch-2].norm.x;
normal.y += mapTris[i-pitch-2].norm.y;
normal.z += mapTris[i-pitch-2].norm.z;
}
if (i % pitch != 509)
{
normal.x += mapTris[i-1].norm.x;
normal.y += mapTris[i-1].norm.y;
normal.z += mapTris[i-1].norm.z;
}

l = sqrtf((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z));
normal.x /= l;
normal.y /= l;
normal.z /= l;

/*Set the new normal to the proper verts*/
mapTris[i].verts.norm = normal;
mapTris[i+1].verts.norm = normal;

if (i - pitch > 0)
{
mapTris[i-pitch].verts.norm = normal;
}
if (i - pitch > 0 && i % pitch != 509)
{
mapTris[i-pitch-1].verts.norm = normal;
mapTris[i-pitch-2].verts.norm = normal;
}
if (i % pitch != 509)
{
mapTris[i-1].verts.norm = normal;
}

}
}```
Are there any blatant mistakes, or might it be something obscure?

Thanks 2. Code:
```>        if (i - pitch > 0 && i % pitch != 509)
>        {
>            mapTris[i-pitch-1].verts.norm = normal;
>            mapTris[i-pitch-2].verts.norm = normal;
>        }```
Let's assume pitch=2 to make it simple. Then the fourth time thru the loop i=3. So i-pitch is 1, which is greater than 0. And i-pitch-2 = -1, which is out of bounds. 3. Ohh I see. I'll see what I can do.

Any idea why there was no seg fault when I included the std::cin.get()?

Thanks 4. Forget what I posted IdioticCreation, I just noticed you increment i by 2, so i would never be 3, so it looks like you're ok. Do you know what the size of mapTris is at this point? 5. Ohh true. I did find the problem when changing what you had first suggested. In this case the pitch was equal to 510. So this:
Code:
`if (i - pitch > 0 && i &#37; pitch != 509)`
should have been this:
Code:
`if (i - pitch > 1 && i % pitch != 0 && i % pitch != 1)`
I think I was just confused when I wrote it at first. If the % is 0 then it is the first one on a row, but since I subtract two, it cant be the first or second one. So the % can't equal 1 either.

Thanks for the help. I probably wouldn't have found it as soon if I hadn't been looking for the other problem.  6. ##  Originally Posted by IdioticCreation Thanks for the help. I probably wouldn't have found it as soon if I hadn't been looking for the other problem. I'm glad you got it working. 7. Try using "at" if you feel you're getting out of bounds errors. It will throw an error if you get out of bounds.
Visual Studio is also helpful enough to throw an assert if you try to access out of bounds elements (with both the index operator [] and at).
Just a little tip. 8. Ohh yeah I recall that now. I didn't make the connection earlier though. Popular pages Recent additions 