Segmentation fault using recursion to find factorial

This is a discussion on Segmentation fault using recursion to find factorial within the C++ Programming forums, part of the General Programming Boards category; Noob here. The title says it all. Code: //using recursion to find the factorial of an entered number #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Segmentation fault using recursion to find factorial

    Noob here.
    The title says it all.

    Code:
    //using recursion to find the factorial of an entered number
    
    #include <iostream>
    using namespace std;
    int x;
    int solution;
    
    int factorial( int counter ) //factorial finding function
    {
    if ( x < counter ) 
    	{
    	factorial ( x + 1 ); //starts at one, runs the function up until x == the entered number.
    	}
    solution = solution * x; //with the exit of every function, solution is multiplied by the value of the argument.
    if ( solution > counter ) //so lets say the numbered entered was 4. it would run the function of 1, 2, 3 than solution (set equal to 4 already) is multiplied by 3, 2, 1.
    	{
    	if ( x == 1 ) { return solution; } //the function == 1 when it starts and when it ends. this tid bit makes sure not to return solution when it starts.
    	}
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    int main()
    {
    x = 1; 
    
    int number;
    int output;
    cin >> number;
    solution = number; //so that the function works multiplying all numbers less than the entered value
    
    output = factorial ( number ); //runs the function with the entered number.
    cout << output << endl;
    }
    The above code gives me compiles fine but gives me a segmentation fault when I run it.
    Any ideas as to why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is recursive, but I can almost guarantee that it will not produce the right result.

    Certainly this looks VERY wrong:
    Code:
    if ( solution > counter )
    ...
    You should not use global variables for recursive functions, it causes all sorts of strange problems.

    It most likely seg-faults because some condition to exit the recursion is never met - e.g. x is always a constant value?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Wow that's an interesting use of globals for a factorial function...

    Here's an example of doing what you're trying to do:
    Code:
    #include <iostream>
    using namespace std;
    
    int factorial(int x)
    {
        if (x == 1)
        {
            return 1;
        }
        return (x * factorial(x - 1));
    }
    
    int main()
    {
        int number;
        int output;
        cin >> number;
        output = factorial(number);
        cout << output << endl;
    }
    ... maybe that'll help you.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    That looks like infinite loopage to me. The first thing you pass into factorial() is the input, then you repeatedly call factorial() with x+1, of which x is global and never changed. Factorial is called, which just repeatedly calls itself with the condition of if (x < x+1). Sounds like the stack and the heap have collided. The stack won.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    thank you guys.
    i feel like an idiot; after someone else points out your flaw it is blatantly obvious.
    i am still very shaky on the basics; i have been programming a grand total of about 6 days.
    i was backwards in my idea of functions. i thought once you hit a return, you would return the value straight to main, but you return the value to wherever the function was called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 07:32 AM
  4. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  5. segmentation fault?
    By kalleanka in forum C Programming
    Replies: 4
    Last Post: 11-20-2006, 03:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21