odd segmentation fault occurence

This is a discussion on odd segmentation fault occurence within the C Programming forums, part of the General Programming Boards category; Hey guys, Purely to help me debug my code i wondered if i could get some help. I have a ...

  1. #1
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72

    odd segmentation fault occurence

    Hey guys, Purely to help me debug my code i wondered if i could get some help. I have a function that returns void and in no way accesses memory. However, upon leaving that function i get a segmentation fault. Im not using pointers, not in a loop. The function simply copies data from one array to another and then manipulates that data. So why might i get such an error upon leaving the function and not during?

    If putting up the code helps then request it, but really I just want to no why this may happen.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    This might have a bazillion of reasons. A look at your code would really help.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    segmentation faults can occur long after (relatively) a memory violation has occured in your code. search your code for violations (especially for malloc, free and array boundaries).

  4. #4
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    okies, the code:
    Code:
    void settriangles()
    {
    float tessellate[12][6]={0};
    int copydata=0, trip=0, count;
    
    for(count=0; count<13; count++)
    	{
    	if(print[count][S]==1)
    		{
    		tessellate[count][X]=print[count][X];
    		tessellate[count][Y]=print[count][Y];
    		tessellate[count][R]=print[count][R];
    		tessellate[count][G]=print[count][G];
    		tessellate[count][B]=print[count][B];
    		copydata++;
    		}
    	}
    
    copydata-=3;
    
    for(count=0; count<=copydata; count++)
    	{
    	trip++;
    	triangle[trip][X1]=tessellate[0][X];
    	triangle[trip][Y1]=tessellate[0][Y];
    	triangle[trip][R1]=tessellate[0][R];
    	triangle[trip][G1]=tessellate[0][G];
    	triangle[trip][B1]=tessellate[0][B];
    	triangle[trip][X2]=tessellate[count+1][X];
    	triangle[trip][Y2]=tessellate[count+1][Y];
    	triangle[trip][R2]=tessellate[count+1][R];
    	triangle[trip][G2]=tessellate[count+1][G];
    	triangle[trip][B2]=tessellate[count+1][B];
    	triangle[trip][X3]=tessellate[count+2][X];
    	triangle[trip][Y3]=tessellate[count+2][Y];
    	triangle[trip][R3]=tessellate[count+2][R];
    	triangle[trip][G3]=tessellate[count+2][G];
    	triangle[trip][B3]=tessellate[count+2][B];
    	}
    
    printf("\nPre Split\n");
    
    for(count=0; count<=copydata; count++)
    	{
    	if(triangle[count][Y1]!=triangle[count][Y2] || triangle[count][Y2]!=triangle[count][Y3])
    		{
    		trip++;
    		printf("\nSplitTriangles called\n");
    		splittriangle(count, trip);
    		}
    	}
    	
    printf("\nLeaving SetTriangles\n");
    
    return;
    }


    thats the function, when ran it goes as far as printing "Leaving.." etc then when it returns to the main, it has a segmentation fault


    &#91;code]&#91;/code]tagged by Salem

  5. #5
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    i wondered about it overwriting the address stack as you said, the code is pretty useless on its own, i'll just have to dry run i guess. I just wondered if it was likely to be me overwriting data, cheers guys

  6. #6
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    damn you guys are good thanks for the array thung that does appear wrong.
    XYRGBS are an enumerated types used to store the values coords x,y with colour RGB and the S is used in a different function to see whether it needs to be used or not.
    Im not too sure about structures so I was doing it that way until i find time to look into them a bit better. Thank you

  7. #7
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    *does happy dance* fixed it! ty very much. i have another error, so be expecting another post in a few hours if i cant sort it :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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