Help with arrays and seg faults

This is a discussion on Help with arrays and seg faults within the C++ Programming forums, part of the General Programming Boards category; I have a function to calculate the normals of vertices in my terrain. I already have the normals for each ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    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;
    
        /*Adds each of the 6 adjacent triangles' normals*/
        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[0].norm = normal;
            mapTris[i+1].verts[0].norm = normal;
    
            if (i - pitch > 0)
            {
                mapTris[i-pitch].verts[2].norm = normal;
            }
            if (i - pitch > 0 && i % pitch != 509)
            {
                mapTris[i-pitch-1].verts[2].norm = normal;
                mapTris[i-pitch-2].verts[1].norm = normal;
            }
            if (i % pitch != 509)
            {
                mapTris[i-1].verts[1].norm = normal;
            }
    
        }
    }
    Are there any blatant mistakes, or might it be something obscure?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >        if (i - pitch > 0 && i % pitch != 509)
    >        {
    >            mapTris[i-pitch-1].verts[2].norm = normal;
    >            mapTris[i-pitch-2].verts[1].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. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    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. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Thumbs up

    Quote Originally Posted by IdioticCreation View Post
    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. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ohh yeah I recall that now. I didn't make the connection earlier though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 10:29 PM
  2. Unknown Seg Fault and Malloc/Realloc Problems
    By DonFord81 in forum C Programming
    Replies: 6
    Last Post: 12-01-2008, 11:49 PM
  3. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 02:07 PM
  4. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 09:22 PM
  5. string arrays
    By lucaspewkas in forum C Programming
    Replies: 2
    Last Post: 04-24-2005, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21