simple C program has "bus error"

Discussion in 'Mac Programming' started by lcriley, Jan 13, 2010.

  1. lcriley macrumors newbie

    Joined:
    Jan 13, 2010
    #1
    I am rather new to programming, and I am trying to write a console program that will create 12 tone matrices.
    The code compiles, but when run I get a "bus error".
    The debugger says EXC_BAD_ACCESS.


    Code:
    
    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 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;}
    	}}
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Look very carefully at this 'for' statement. You may even need to change the font.

    The condition is "one less than eleven". Which is always true. Forever.

    Never name a variable "l" (ell). Never. Ever.

    EDIT: Anddon'tbeafraidtoaddspaces.Itmakesiteasiertospoterrors.
    Code:
    for ( l=1;  1 <= 11;  l++ )
     
  3. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3

    I heard a story about a FORTRAN programmer at a place I worked at years ago who named variables with combinations of O and 0. So he had things like this

    Code:
    O00 = OO0+OOO
    They fired him. Writing l<=1 is almost as bad although I'm sure it was unintended.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Code:
    #include <stdio.h>
    
    void mat(int twelve[13][13]);
    
    int main (int argc, char *argv[]) {
    	int dim1,dim2;
    	int twelve[13][13];
    	
    	printf("input original tone row \n");
    	for(dim1=0;dim1<=11;dim1++) {
    		scanf("%2i",&twelve[dim1][0]);
    	}
    	
    	mat(twelve);
    	
    	for(dim2=0;dim2<=11;dim2++) {
    		for(dim1=0;dim1<=11;dim1++) {
    			printf("%i ",twelve[dim1][dim2]);
    		}
    		printf("\n");
    	}
    	return 0;
    }
    		
    void mat(int twelve[13][13]) {
    	int temp,dim1,dim2;
    	
    	/*inversion*/
    	for(dim1=1;dim1<=11;dim1++) {
    		twelve[0][dim1] = 12 - twelve[dim1][0];
    	}
    	/*fill in columns*/
    	/*this sections seems to be what's crashing it */
    	for(dim1=1;dim1<=11;dim1++) {
    		for(dim2=1;dim2<=11;dim2++) {
    			temp = twelve[0][dim1] + twelve[dim2][0];
    			if(temp >= 12) {
    				twelve[dim1][dim2] = temp - 12;
    			} else {
    				twelve[dim1][dim2] = temp;
    			}
    		}
    	}
    }
    
    This should be easier to read, and work. People have already told you and showed you the error... but what did you try to debug this? Did you add any print statements to see what was going on? Did you use the debugger? For more complex problems, just a quick look-see won't do it. You should start using tools to debug your code now.

    -Lee
     
  5. lcriley thread starter macrumors newbie

    Joined:
    Jan 13, 2010
    #5
    Thanks for your help. I can't believe I didn't spot such a simple mistake.
    Is there a suggested font to use for programming?

    I did use the debugger in xcode, but I'm not quite sure I understood everything it was saying.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    The default Terminal font in OS X is Menlo. It suits me fine, day-to-day.

    The attached image is a screenshot of a vim session with some l's and 1's. They are pretty easy to tell apart in this font. XCode uses Monaco by default, and it's pretty easy to tell there, too. If you're using an earlier version it may be different, though.

    -Lee
     

    Attached Files:

  7. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #7
    First, if you need to change the font to prevent coding mistakes, you're doing it wrong. Use things like i,j,k, or x,y,z but not j,k,l.

    Next, you'll easily spend as much time debugging as you do writing code. You don't actually have to use a debugger to debug code, but it does make it easier, since you don't have to write a new printf statement and recompile just to see the value of a different variable. If you can't debug your own code, let alone someone else's, you won't be a useful programmer. That being said, sometimes you do just need an extra set of eyes with a different perspective.
     
  8. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    http://en.wikipedia.org/wiki/Fortran#Letter_O_considered_harmful

    "Letter O considered harmful"
    Someone proposed taking the capital-o out of the character set.
     

Share This Page