Thread: Seg Fault during recursion

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    Seg Fault during recursion

    Hello Folks, I made a program with a constantly recursion. It all works but at a certain moment i throws me a seg fault, but i cant figure out why. (maybe of the constantly recursion?)

    recursive function
    Code:
    int window::move(int xposition,bool rightorleft)
    {
    	//direction: true/right,false/left
    	bool direction = rightorleft; 
    	int currentposition = xposition;
    	
    	if(direction)
    	{
    		//we go right:
    		if(currentposition >= this->returnwindowwidth())
    		{
    			direction = false;
    			std::cout << "currentposition is:\t" << currentposition << "\tnext direction: Left" << std::endl;
    			return move(currentposition,direction);
    		}
    		else
    		{
    			direction = true;
    			std::cout << "currentposition is:\t" << currentposition << "\tnext direction: Right" << std::endl;
    			return move(currentposition+8,direction);
    		}
    	}
    	else
    	{
    		//we go left:
    		if(currentposition <= 0)
    		{
    			direction = true;
    			std::cout << "currentposition is:\t" << currentposition << "\tnext direction: right" << std::endl;
    			return move(currentposition,direction);
    		}
    		else
    		{
    			direction = false;
    			std::cout << "currentposition is:\t" << currentposition << "\tnext direction: Left" << std::endl;
    			return move(currentposition-8,direction);
    		}
    	}
    }
    implementationrecursive function starts when I press the button)
    Code:
    void window::button_clicked()
    {
    	std::cout << "de breedte is:\t" << this->returnwindowwidth() << std::endl;
    	std::cout << "we are starting:\t" << "position 0, direction: right" << std::endl;
    	sleep(10);
    	move(0,true);
    }
    does somebody of you knows what I doing wrong?

    Edit:

    Language: C++
    IDE: Monodevelop.
    OS: Ubuntu 9.10.

    thanks for your help!
    The Programming Dutchman

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every recursive call requires a little bit more memory; and since this function always calls itself you will run out of memory at some point.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    It seems like you are recursing forever. You can't keep calling the same function forever or you run out of memory. I think you may be looking to implement a loop and not use recursion for your purpose.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > does somebody of you knows what I doing wrong?
    Yes, your code has no "base case" that is trivial to solve without involving recursion.

    Eg
    Code:
    int fact ( int n ) {
      if ( n <= 1 ) {
        return n;  // the trivial base case
      } else {
        return n * fact( n - 1 );
      }
    }
    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.

  5. #5
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Runs out of memory.... Yeah that is quite silly of me.

    Ok, so recursion is not the way to do it? just loop it?
    The Programming Dutchman

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you could turn it into an infinite loop and at least you won't run out of memory.

    But if you don't want an infinite loop, then as Salem says, you'll need a stop condition.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Oke, this helped me a lot. Thanks.
    The Programming Dutchman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf seg fault
    By mercuryfrost in forum C Programming
    Replies: 19
    Last Post: 08-20-2009, 01:22 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM