简体   繁体   中英

Stack Overflow Error With This Recursive Program? - C++

I'm a programming student in my first C++ class, and recently we were given an assignment to implement a recursive program that finds the first occurrence of a given substring in a given string.

For example:

int StringIndex("Mississippi", "sip"); // this would return 6

The hint we are given is to use a recursive helper function that takes the index as a parameter.

Here is what I've done so far:

int index_of(string s, string t)
{
    int index = 0;

    if (s[index] == NULL)
        return -1;
    else if (starts_with(s, t, ++index))
    {
        return index;
    }
    return index_of(s, t);
}

bool starts_with(string s, string t, int index)
{
    if (t[index] != s[index] || s[index] == NULL)
        return false;
    return starts_with(s, t, ++index);
}

I am getting a stack overflow error, and I don't understand why. So would somebody mind helping me understand why I'm getting this error? And help me get pointed in the right direction as to how to fix it?

int index_of(string s, string t)
{
    ...

    // Your are calling yourself without modifying the
    // input parameters.  If you get here once, you'll
    // never break out.
    return index_of(s, t);
}

Nothing changes s or t so this can never stop.

When writing recursive functions you should always keep two things in mind: you need stop conditions which end the recursion and you have to get closer to a stop condition with each function call. If you fail to check stop conditions or if your function doesn't get closer to a stop condition during each call, you'll run into stack overflows (or infinite loops).

The problem in your first function is that it doesn't get closer to the stop condition. At the end you "return index_of(s, t)" without modifying s or t in the meantime. So the function will start over with the same parameters, again and again until you run into a stack overflow.

Another problem is in your starts_with function. It only returns false but never true.

When you say:

s[index] == NULL

you should be aware that C++ std::strings need not be null-terminated internally - use the string's size() member function instead instead. Also, you are passing the strings by value, which for long strings could rapidly eat up stack space and dynamic memory . Pass them as const references instead:

bool starts_with( const string & s, const & string t, int index)
{
   ...
}

And lastly, as others have pointed out, only ONE of the functions, in this case starts_with() should be recursive. In fact, index_of() seems rather pointless.

A) "starts_with" NEVER returns a true. So you never exit the index_of recursion.
B) index_of doesn't do anything on the recurse it just calls itself again with the same information.

Basically the 2 above problem leads to an infinite loop. You constantly check the same information and don't have the possibility of exiting that loop.

If you'll pardon a way of pointing out the error that some might consider just a little too cute, consider the difference between:

See this answer

and:

If you don't understand see this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM