Stack overflow in recursion

This is a discussion on Stack overflow in recursion within the C Programming forums, part of the General Programming Boards category; Hi everyone! I'm trying to write a function in c which will receive a logical sentence and return whether it ...

  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
    22,304
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12:09 PM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 04:20 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  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

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