# Thread: odd segmentation fault occurence

1. ## 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. This might have a bazillion of reasons. A look at your code would really help.

3. 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. 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. 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. 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. *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