Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post Segmentation fault

    Can anyone check ths code and tell me whats wrong? Im getting a Segmentation fault [COREDUMP] when im runnig my program. I do not get any error when compiling.

    Code:
    #include <stdio.h>
    int size(void)
    {
        int n; //size of matrix        
        printf ("What is the size of the matrix?"); //user input
        scanf ("%d",&n);
        return n;  
    }
    int recursion (int row, int column, int n) //Recursive function
    {
        int result;
         
        result = ((row + 1) + column) % n;
        
        // If the result is 0, n will be returned. n is the last digit in the   
            series
        if (result == 0) 
        {
            return n;
        }
        else
        {
            return result;
        }
    }
    int main ()
    {
    
        int row; //row of the matrix
        int column; // column of the matrix
        int n; // n is the size of the matrix as mentioned in the question 
        int square[n][n];
        
        n = size();
       
        for (row = 0; row < n; row++)
        {
             printf("\n");
    
             for (column = 0; column < n; column++)
             {
                  square[row][column] = recursion(row, column, n);
                  printf("%d", square);   // Prints the Result         
             }     
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post Also,

    In the program, I have to build a Latin square for n elements.
    Eg of latin square for n=3;

    1 2 3
    2 3 1
    3 1 2

    I also get a warning, no new line at end of file.
    What does that mean?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Code:
    #include <stdio.h>
    int size(void)
    {
        int n; //size of matrix        
        printf ("What is the size of the matrix?"); //user input
        scanf ("%d",&n);
        return n;  
    }
    int recursion (int row, int column, int n) //Recursive function
    {
        int result;
         
        result = ((row + 1) + column) % n;
        
        // If the result is 0, n will be returned. n is the last digit in the   
        /*What a heck is series?*/ 
    		//   series
        if (result == 0) 
        {
            return n;
        }
        else
        {
            return result;
        }
    }
    int main ()
    {
    
        int row; //row of the matrix
        int column; // column of the matrix
        int n; // n is the size of the matrix as mentioned in the question 
        int square[n][n];//this is invalid!
        
        //n = size();this makes no sence
       //you need something like
    	 /*
    		* int *foo = calloc(n,sizeof(int*));
    		* foo[i] = calloc(n,sizeof(int));
    		*/
        for (row = 0; row < n; row++)
        {
             printf("\n");
    
             for (column = 0; column < n; column++)
             {
                  square[row][column] = recursion(row, column, n);
                  //This is wrong you can't print the whole matrix at once
    							printf("%d",square[?][?]);   // Prints the Result         
             }     
        }
        return 0;
    }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What's so recursive about your recursion() function?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post

    This is the change I made. I still get the Segmentation Fault error as well as no new line at end of file warning. Please check.

    Code:
    #include <stdio.h>
    int asksize(void)
    {
        int n; //size of matrix        
        printf ("What is the size of the matrix?"); //user input
        scanf ("%d",&n);
        return n;  
    }
    int recursion (int row, int column, int n) //Recursive function
    {
        int result;
         
        result = ((row + 1) + column) % n;
        
        // If the result is 0, n will be returned. n is the last digit in the   
            series// Series means from 1 to 10, 10 is the last digit. So if a user inputs the size. the square generates from 1 to n, where n is the last digit.
        if (result == 0) 
        {
            return n;
        }
        else
        {
            return result;
        }
    }
    int main ()
    {
    
        int row; //row of the matrix
        int column; // column of the matrix
        int n; // n is the size of the matrix as mentioned in the question 
        int square[n][n]; //What is wrong with this declaration, as  
                                    the  user will be giving the size of the array
        
        n = asksize();
       
        for (row = 0; row < n; row++)
        {
             printf("\n");
    
             for (column = 0; column < n; column++)
             {
                  square[row][column] = recursion(row, column, n);
                  printf("%d", square[row][matrix]);   // Prints the Result         
             }     
        }
        putchar('\n');
        return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post

    Im a beginner in C. So that seems like a recursive function to me. Aint it?

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    recursive functions are of this format:

    Code:
    returntype function (some arguments)
    {
        /*some code*/
        function (arguments);
    }
    naturally your function isn't doing that: recursion - a function
    calling itself.

    no newline at end of file means to go to the very last line of code
    (i.e. the last brace in the file) and press enter - save - recompile.

    this code should not compile - for one I quote my compiler:
    >>1>c:\documents and
    settings\rich\desktop\boredom\boredom\boredom.cpp( 44) : error
    C2065: 'matrix' : undeclared identifier

    also,

    >>int square[n][n]; //What is wrong with this declaration, as
    //the user will be giving the size of the array

    See this thread

    quzah pointed out that it is legal in C99 to some extent, but
    you won't find many compilers that comply to that code yet - use
    malloc, or make the array a constant size
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int n; // n is the size of the matrix
    Setting aside the legality (or otherwise) of the way you declare your array, you need to read the size in BEFORE you create the array.
    At the moment, you've probably got some huge (or negative) random value (because n isn't initialised) and you try and create some totally bogus array with that size.

    Go with something like
    Code:
    #define MAXSIZE 10
    And
    Code:
    int square[MAXSIZE ][MAXSIZE ];
    n = asksize();
    if ( n > MAXSIZE ) {
      printf( "That's too big for me\n" );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    4
    Hi Ron

    The problem is with your array. square[n][n] is not accepted in C.

    I have modified your main function which now uses dynamic allocation of two dimentional array.

    Compile this code and it should work for you.

    ------------------------------------------------

    Code:
    #include <stdio.h>
    
    int size(void)
    {
        int n; /* size of matrix */
        printf ("What is the size of the matrix: "); /* user input */
        scanf ("%d",&n);
        return n;
    }
    
    
    int recursion( int row,  int column,  int n) /* Recursive function */
    {
        int result;
    
        result = ((row + 1) + column) % n;
    
        /* If the result is 0, n will be returned. n is the last digit in the  series */
        if (result == 0)
        {
            return n;
        }
        else
        {
            return result;
        }
    }
    
    
    int main( )
    {
    
        int row; /* row of the matrix */
        int column; /*  column of the matrix */
        int n; /*  n is the size of the matrix as mentioned in the question  */
        int **square; 
    
        n = size();
    
        /* Memory allocation for the matrix.... */
        square = (int **) malloc( n * sizeof(int));
    
        for (row = 0; row < n; row++)
        {
             for (column = 0; column < n; column++)
             {
                  square[row] = (int *) malloc( n * sizeof(int));
             }
        }
    
        for (row = 0; row < n; row++)
        {
             printf("\n");
    
             for (column = 0; column < n; column++)
             {
                  square[row][column] = recursion(row, column, n);
                  printf("%d ", square[row][column]);   /* Prints the Result */
             }
        }
        printf("\n\n");
    
        free(square);
    
        return 0;
    }
    Regards
    Ravi O. Rattihalli
    [email protected]

  10. #10
    Registered User
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    4

    small change !!!

    Hi Ron

    Further to my previous message.....

    There is no need to have two for loops while allocationg memory!!
    I saw that I have used two loops just now...

    so it should be like this:

    Code:
        /* Memory allocation .... */
        square = (int **) malloc( n * sizeof(int));
    
        for (row = 0; row < n; row++)
        {
            square[row] = (int *) malloc( n * sizeof(int));
        }
    then the other for loop follows....


    Cheers

    Ravi O. Rattihalli

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > square = (int **) malloc( n * sizeof(int));
    Try it without the cast, and see what error message you get.

    Then read this
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    and never cast malloc again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    4

    thats fine but........

    Yup.... I am aware of the warning :

    Operation between types "int**" and "int" is not allowed..

    But then how will you avoid that warning?? without casting it?

    In such cases the documentation helps the developer whenever the declaration type is changed... so that he will change the cast accordingly... ( at the time of maintenance )

    But the idea of using "sizeof *p" in malloc is very good.

    If you know any best way of using malloc without cast and still avoid warnings, please let me know..

    BTW, I had given the solution to Ron's problme so that he can atleast get rid of the segmentation fault and see the latin square :-)

    Regards
    Ravi

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    But then how will you avoid that warning?? without casting it?
    By compiling your C programs as C instead of C++. As long as you're including the right header file (stdlib.h) then C won't complain.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    4

    got it right this time !!!

    Hi Salem....

    I guess you are right.. Need not cast the malloc if I use stdlib.h

    and "itsme86" , thanX for the suggestion.. but saw ur mail after I corrected my code ;-) but I must thank you too.....

    so Ron !!

    here is your changed main again

    Code:
    int main(void)
    {
    
        int row; /* row of the matrix */
        int column; /*  column of the matrix */
        int n; /*  n is the size of the matrix as mentioned in the   question  */
    
        n = size();
        int **square;
    
        /* Memory allocation for the matrix.... */
        square = malloc( n * sizeof(**square));
        for (row = 0; row < n; row++)
        {
            square[row] = malloc( n * sizeof(**square));
        }
    
        for (row = 0; row < n; row++)
        {
             printf("\n");
    
             for (column = 0; column < n; column++)
             {
                  square[row][column] = recursion(row, column, n);
                  printf("%d ", square[row][column]);   /* Prints the Result */
             }
        }
        printf("\n\n");
    
        free(square);
    
        return 0;
    }
    Regards
    Ravi

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Nope, still wrong
    Code:
        square = malloc( n * sizeof(*square));  /* only one * here */
        for (row = 0; row < n; row++)
        {
            square[row] = malloc( n * sizeof(*square[row]));  /* ** is OK, but not so intuitive */
        }
    Always p = malloc ( n * sizeof *p );

    > free(square);
    You need to free each square[row] first
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM