Segmentation fault in code under 10.4 however not present in same code in 10.6

Discussion in 'Mac Programming' started by tom1992, Aug 26, 2010.

  1. tom1992 macrumors newbie

    Joined:
    Aug 26, 2010
    #1
    Hi, i'm still new to programming. I have been trying to learn C programming for basic command line tools in mac, however have come up against a problem. I have a unibody macbook and an imac G4. I wrote some code on my Macbook and it compiled fine and works on that however when I compile the same source file under gcc or xcode on my imac G4 (10.4.11) I get a segmentation fault. I have also tried compiling under Ubuntu linux however I also get a segmentation fault. I have not included any complex memory allocation and cannot figure what the problem is. I was hoping some seasoned developer may be able to point out in simplistic terms where abouts I have gone wrong.

    Thanks in advance and any pointers would be appreciated (pun intended :) )

    Code:
     
    
    /*
     
     Darts Source Code is Licensed under a creative commons license. license shown below:
     
     http://creativecommons.org/licenses/by-nc-sa/3.0/
     
     All derivatives of this work to be attributed to thomas @ http://digital-focus.info
     
     Email: thomas@digital-focus.info
     
     
     TODO'S 
      - remove function scanf() and replace with fscanf() or fgets() once got to in book, to stop buffer overflow.
     
     */
    
    #include <stdio.h> 
    
    // Flush Input Buffer
    void	Flush( void ) 
    
    {
    	
    	while ( getchar() != '\n' )
    		;
    	
    }
    
    
    // Initiate main program
    int main (int argc, const char * argv[]) 
    
    {
        
    	// Global Variable Definition
    	int players;
    	int test1;
    	int test2;
    	int test3;
    	
    	// Global Variable Definition (Arrays)
    	char name[players][15];
    	long int score[players];
    	
    	// No of Players (Validation included)
    	test1 = 0;
    	while (test1 != 1) 
    		
    	{
    		
    		printf ( "\nEnter number of players: " );
    		if ( scanf ( "%d", &players ) != 1 )
    			
    		{
    			
    			printf("Invalid Input");
    		    Flush();
    			
    		}
    		
    		else 
    			
    		{
    			
    			test1 = 1;
    			
    		}
    		
    	}
    	
    	printf("\n");
    	
    	// Declare loop counter i
    	int i;
    	i = 0;
    	
    	// Loop for player names
    	while (++i < (players + 1)) 
    	
    	{
    		
    		printf ("Enter player %d's name: ", i); 
    		scanf( "%14s", name[i-1]);
    	
    	}
    	
    	// Declare loop counter j and points for game
    	int j;
    	long int points;
    	
    	// No of points for game (Validation included)
    	test2 = 0;
    	while (test2 != 1) 
    		
    	{
    		
    		printf ( "\nEnter number of points to start from: " );
    		if ( scanf ( "%6ld", &points ) != 1 )
    			
    		{
    			
    			printf("Invalid Input");
    		    Flush();
    			
    		}
    		
    		else 
    			
    		{
    			
    			test2 = 1;
    			
    		}
    		
    	}
    	
    	
    	// Loop to set scores
    	for (j=0; j<players; j++) 
    	
    	{
    	
    		score[j] = points;
    	
    	}	
    	
    	// Declare loop counter j and variables for arithmetic
    	int k;
    	long int less;
    	long int temp;
    	
    	// Loop to track scores until a score is 0 then release. (Validation included)
    	for (k=0; k<(players +1); k++) 
    	
    	{
    		
    		temp = score[k];
    		test3 = 0;
    		while (test3 != 1) 
    			
    		{
    			
    			printf ( "\n\n%s's turn - score: %ld\n\nPoints earned: " , name[k], score[k]);
    			if ( scanf ( "%6ld", &less ) != 1 || (temp - less) < 0)
    				
    			{
    				
    				printf("Invalid Input");
    				Flush();
    				
    			}
    			
    			else 
    				
    			{
    				
    				test3 = 1;
    				
    			}
    			
    		}
    		
    		score[k] = temp - less;
    		printf("\nNew Score: %ld\n",score[k]);
    		if (score[k] ==0 ) 
    		
    		{
    			
    			printf("\n\n\n%s has won!\n\n", name[k]);
    			break;
    		
    		}
    		
    		if (k== (players -1)) 
    		
    		{
    			
    			k=-1;
    		
    		}
    
    	}
    
    	return 0;
    
    }
    
     
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    players is uninitialized when you use it to declare name and score. I think being able to use a variable as a dimension of an array is an extension, for one, but for two, god only knows what's in the memory that players is using. Hence, I think there is stack destruction after these variables are declared.

    -Lee

    EDIT: Reading the rest of the code, I am frightened by what I see. Is this a school project? Was some code provided for you, and you filled in the rest? The use of while/for is inconsistent, the manipulation of k inside the last for loop is ill-advised. You may not have come for a critique, but it's best to fix problems like this early instead of limping along trying any syntactical and stylistic shenanigans necessary to just "make it work".
     
  3. tom1992 thread starter macrumors newbie

    Joined:
    Aug 26, 2010
    #3
    Thanks a lot, this worked. I'm still at a loss as to how this worked in xcode and gcc in 10.6. maybe they have error checking etc. Only been programming for about 1.5 weeks so it's little things like this that trip me up :). i'm going to continue to hone this code and replace the validation with a validator function I have been working on. Thanks again, you've saved me a lot of head scratching

    I'm still very new to the world of c. I have a book which I'm at the stage of learning for, while and if functions. This program was a test of these new functions and a test to add type char as I had been doing integer and floating point calculations before. It's really ugly i know and i'm trying to replace the validation parts with a seperate function which i used in another project. I'm basically following this book and then testing what i can do using what i have learned to solve problems that i have previously been using applescript for.

    I want to modify the validator function from this program to work in the darts program.

    Code:
    
    /*
    
     
     Inverse 3 x 3 matrix Source Code is Licensed under the GPL v2.0 license. license shown below:
     
     http://www.gnu.org/licenses/gpl-2.0.html
     
     All derivatives of this work to be attributed to thomas @ http://digital-focus.info
     
     Email: thomas@digital-focus.info
     
     
     TODO'S 
     - remove function scanf() and replace with fscanf() or fgets() once got to in book, to stop buffer overflow.
     
     
    */
    
    // includes
    
    #include <stdio.h>
    
    
    // function declaration
    
    void Determinant3X3Matrix( void );
    void Inverse3X3Matrix( void );
    void Validator( char text[15], double *input );
    void Flush(void);
    void Exit(void);
    
    // variables definition
    
    double a, b, c, d, e, f, g, h, i, det;
    
    // start main function
    
    int main (int argc, const char * argv[]) 
    
    {
    	
        Determinant3X3Matrix();
    	Inverse3X3Matrix();
    	Exit();
    	return 0;
    	
    }
    
    // end main function
    
    
    
    // begin Determinant 3 X 3 Matrix function
    
    void Determinant3X3Matrix( void ) 
    
    {
    	
    	// selection of variables
    	
    	printf("\nenter matrix as\n\n{a, b, c}\n{d, e, f}\n{g, h, i}\n\n");
    	
    	// Selection using function Validator
    	
    	Validator ( "a = " , &a );
    	Validator ( "b = " , &b );
    	Validator ( "c = " , &c );
    	Validator ( "d = " , &d );
    	Validator ( "e = " , &e );
    	Validator ( "f = " , &f );
    	Validator ( "g = " , &g );
    	Validator ( "h = " , &h );
    	Validator ( "i = " , &i );
    	
    	
    	// calculate determinant
    	
    	det = ((a * e * i) - (a * f * h) + (b * f * g) - (b * d * i) + (c * d * h) - (c * e * g));
    	
    	
    }
    
    // end Determinant 3 X 3 Matrix function
    
    
    // begin Inverse 3 X 3 Matrix function 
    
    void Inverse3X3Matrix( void ) 
    
    {
    	// check if matrix is singular
    	
    	if (det==0) 
    	
    	{
    		
    		// if singular stop program
    		
    		printf("\nDeterminant = 0\n\n");
    	
    	}
    	
    	else 
    	
    	{
    		
    		// variables definition
    		
    		double  a1, b1, c1, d1, e1, f1, g1, h1, i1;
    		
    		
    		// matrix of minors
    		
    		a1 = ((e * i) - (f * h));
    		b1 = ((d * i) - (g * f));
    		c1 = ((d * h) - (e * g));
    		d1 = ((b * i) - (c * h));
    		e1 = ((a * i) - (c * g));
    		f1 = ((a * h) - (b * g));
    		g1 = ((b * f) - (e * c));
    		h1 = ((a * f) - (c * d));
    		i1 = ((a * e) - (b * d));
    		
    		// reassign values from matrix of minors
    		
    		a = a1;
    		b = b1;
    		c = c1;
    		d = d1;
    		e = e1;
    		f = f1;
    		g = g1;
    		h = h1;
    		i = i1;
    		
    		// matrix of cofactors
    		
    		b = -b;
    		d = -d;
    		f = -f;
    		h = -h;
    		
    		// transpose matrix
    		
    		b1 = d;
    		c1 = g;
    		d1 = b;
    		f1 = h;
    		g1 = c;
    		h1 = f;
    		
    		// reassign values from transpose matrix
    		
    		b = b1;
    		c = c1;
    		d = d1;
    		f = f1;
    		g = g1;
    		h = h1;
    		
    		// calculate 1/det
    		
    		det = 1 / det;
    		
    		// matrix * 1 / det
    		
    		a = a * det;
    		b = b * det;
    		c = c * det;
    		d = d * det;
    		e = e * det;
    		f = f * det;
    		g = g * det;
    		h = h * det;
    		i = i * det;
    		
    		// print inverse matrix
    		
    		printf("\n Inverse Matrix:\n\n{ %.05f , %.05f , %.05f }\n{ %.05f , %.05f , %.05f }\n{ %.05f , %.05f , %.05f }\n\n", a, b, c, d, e, f, g, h, i);
    		
    	}
    
    		
    }
    
    // end Inverse 3 X 3 Matrix function
    
    
    // begin Flush function (flush input buffer)
    
    void	Flush( void ) 
    
    {
    	
    	while ( getchar() != '\n' )
    		;
    	
    }
    
    // end Flush function
    
    // begin Validator function (Checks if input type is double)
    
    void	Validator( char text[100], double *input ) 
    
    {
    	int test1;
    	test1 = 0;
    	while (test1 != 1) 
    		
    	{
    		
    		printf ( "%s", text );
    		if ( scanf ( "%lf", &*input ) != 1 )
    			
    		{
    			
    			printf("Invalid Input\n");
    			Flush();
    			
    		}
    		
    		else 
    			
    		{
    			
    			test1 = 1;
    			
    		}
    		
    	}
    	
    }
    
    // end Validator function
    
    // begin Exit function
    
    void Exit( void ) 
    
    {
    
    	printf ( "Press ENTER to exit . . ." );
    	Flush();
    	getchar();
    	
    }
    
    // end Exit function
    
    
     

Share This Page