xcode - bus error in simple Mac OSX C program -
i writing simple c program create twelve tone matrix. code compiles, run time error 'bus error'. in debugger says exc_bad_access
.
int main () { int j,k,l; int twelve[13][13]; void mat(int twelve[13][13]); printf("input original tone row \n"); for(j=0;j<=11;j++) { scanf("%2i",&twelve[j][0]); } mat(twelve); for(k=0;k<=11;k++) { for(l=0;l<=11;l++) { printf("%i ",twelve[l][k]); } printf("\n"); } return 0; } void mat(twelve) int twelve[13][13]; { int j,k,l; int temp; /*inversion*/ for(j=1;j<=11;j++) { twelve[0][j] = 12 - twelve[j][0]; } /*fill in columns*/ /*this sections seems what's crashing */ for(k=1;k<=11;k++) { for(l=1;1<=11;l++) { temp = twelve[0][k] + twelve[l][0]; if(temp >= 12) { twelve[k][l] = temp - 12; } else { twelve[k][l] = temp; } } } }
there typo in inner loop condition of mat() subroutine.
this why don't 'l' (el) index.
for(l=1;1<=11;l++)
you meant "l < 11" (el) not "1 < 11" (one)
one less eleven, l (el) index increases without bound, leads illegal memory access when index gets large.
Comments
Post a Comment