开发者

bus error in simple Mac OSX C program

开发者 https://www.devze.com 2022-12-17 09:45 出处:网络
I am writing a simple C program to create a twelve tone matrix. The code compiles, but I get run time error \'Bus Error\'.

I am writing a simple C program to create a twelve tone matrix. The code compiles, but I get run time error 'Bus Error'. In the debugger it 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[1开发者_JAVA百科3][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 to be what's crashing it */
    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 is a typo in the inner loop condition of the mat() subroutine.

This is why I don't like 'l' (el) as an index.

for(l=1;1<=11;l++)

you meant "l < 11" (el) not "1 < 11" (one)

One is always less than eleven, so the l (el) index increases without bound, which leads to an illegal memory access when index gets too large.


About 10 lines up from the bottom of your code you have the statement:

{for(l=1;1<=11;l++)

should that be

{for(l=1;l<=11;l++)
0

精彩评论

暂无评论...
验证码 换一张
取 消