Thread: Stack overflow in recursion

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Stack overflow in recursion

    Hi everyone!

    I'm trying to write a function in c which will receive a logical sentence and return whether it is legal or not.

    My fuction is recursive and at the first call it crashes with the message "Stack overflow" in visual studio.

    here is a part of my code:

    Code:
    char* checkSent(char sent[])
    {
    
    	int wasBracket=0, brackets=0;
    	
    		if (sent=='\0')
    		{
    			if(brackets)
    			{
    				fail();
    			}
    			else return sent;
    		}
    		
    		sent=checkSent(sent++);
    I will appreciate any help or information you can offer me about how to solve this and why this is happening.

    Thanks in advance,
    Rangid.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    sent=checkSent(sent++);

    Perhaps send = checkSent(sent+1); ?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Quote Originally Posted by Bayint Naung View Post
    sent=checkSent(sent++);

    Perhaps send = checkSent(sent+1); ?
    Thanks for the response, but unfortunately that does not solve my problem.

    I have changed the line

    sent = checkSent(sent++);

    to:

    strcpy(sent, checkSent(sent++));

    but still the overflow error persists.

    Any other suggestions?

    Thanks.
    Last edited by Nom1fan; 11-24-2010 at 11:03 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If checkSent is supposed to "return whether it is legal or not", why does it return a pointer to char rather than an int that serves as a boolean value?

    Note that this is wrong because you modify sent twice between consecutive sequence points, resulting in undefined behaviour:
    Code:
    sent=checkSent(sent++);
    This is wrong because the order of evaluation of function arguments is implementation defined:
    Code:
    strcpy(sent, checkSent(sent++));
    Actually, it is probably also logically wrong, but I find it hard to express this other than by asking: what in the world are you trying to do with that statement?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, use +1 like was suggested.

    sent++ passes the ORIGINAL value of sent to the recursive invocation.
    In other words, you're doing nothing with the value being passed.

    sent+1 on the other hand makes progress towards some target.
    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. Callback recursion: stack overflow
    By nicoqwertyu in forum C++ Programming
    Replies: 8
    Last Post: 03-16-2010, 11:09 AM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM