Thread: Recursion Function - OverStacked (Help!)

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Unhappy Recursion Function - OverStacked (Help!)

    I have to implement a recursive function. But I got stuck there.

    The function I write is like that:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int strlenRecursive(char* str) {

    if (str == '\0')
    return 0;

    else
    return strlenRecursive(str-1);
    }

    int main() {

    char buffer[256];

    printf("Enter a string and I will tell you the length (CTRL-Z to quit)\n");
    while (scanf("%s", buffer) != EOF) {
    printf("The Length of '%s' is %d\n", buffer, strlenRecursive(buffer));
    }

    fflush(stdin);
    getchar();
    }

    I have no idea on how to solve it which took me nearly one week!.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly is your function supposed to do? You realize that you're passing it a pointer to a string, and then going BACKWARDS from the START of it, right? You did know that, right?

    Ok, you have no idea what you're doing here, so I'll help. In a nutshell, you want to keep track of some number and return that as your total length.
    Code:
    int recursivestrlen( char *s, int x )
    {
        if( !s+x ) return 0;
        return recursivestrlen( s, x+1 );
    }
    That should do it. Call the function:

    recursivestrlen( myString, 0 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    I would bet money that you are getting the stack overflow because you have a function calling itself the same number of times as the length of the string (if the code worked) and each time new variables are created, etc, solution is to use a loop in a function so that it is only called once, enjoy:

    #include <stdio.h>
    #include <stdlib.h>

    int strlenRecursive(char *str) {
    int counter=-1;
    while(str[++counter]!='\0');
    return counter;
    }

    int main() {
    char buffer[256];

    do{
    printf("Enter a string and I will tell you the length (Q to quit)\n");
    gets(buffer);
    if(buffer[0]!='Q' || buffer[1]!='\0')
    printf("The Length of '%s' is %d\n\n", buffer, strlenRecursive(buffer));
    } while(buffer[0]!='Q' || buffer[1]!='\0');
    return 1;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I would bet money that you are getting the stack overflow
    > because you have a function calling itself the same number of
    > times as the length of the string (if the code worked) and each
    > time new variables are created, etc

    I'll take your bet.

    1) The function doesn't work.
    2) Here is what happens:
    Code:
    func( stringAddy )
       func( stringAddy - 1 )
          func( stringAddy - 2 )
             func( stringAddy -3 )
                ...and on and on and on...
    He isn't decrementing a counter. He's decrementing the pointer's value. As such, he keeps making the value point some place else in memory. God only knows where he ends up pointing.

    3) No variables are actually declared in his function. Therefore, it takes him a long long time to actually overflow stack space this way (a few seconds at most )

    Anyway, you're close to being right. He eventually runs out of stack space, or more likely, tries accessing some point in memory that he doesn't have access to.

    Furthermore, do you know what recursion is? Your example is NOT recursive, which is the whole point of his example/lesson.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    yeah probly right, but either way having a function call its self for an unknown number of times is a bad idea in my opinion.

  6. #6
    Unregistered
    Guest

    Smile

    oops fair enough, looks like your answers right

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Question

    Thank you, I have solved it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM