Resun
Resun

Reputation: 3

Large Number of Function Recursions - C++

I have a project that requires different types of iterations. One is function recursion on a char array of 5000. After 50 calls it will crash, assuming from stack overflow. Not sure how to get around this.

void functionLoop(int loopInt)
{
#ifdef ___EXSTR_H___
#undef ___EXSTR_H___
#endif
#include "exstr.h"

    ofstream fout;
    fout.open("output.txt");

    int arrayLength =  sizeof ( example_strings ) / 4; // arrayLength = 5000.
    char *stringArray = example_strings[loopInt];
    int charCount = 0;
    while( *stringArray != 0 )
    {
        stringArray++;
        charCount++;
    }
    cout << loopInt + 1 << ": " << charCount << ": " << example_strings[loopInt] << endl;
    loopInt++;
    if(loopInt < arrayLength)
    {   
        functionLoop(loopInt);      
    }
}

EDIT:

I cleaned up the code a lot, got rid of all the variables, moved the header file to a parameter, and gained about 4500 more iterations, but it's still crashing after 4546. Here's the updated code:

void functionLoop(char * example_strings[], ofstream &outputFile, int counter)
{     
    outputFile << counter + 1 << ": " << strlen(example_strings[counter]) << ": " << example_strings[counter] << endl;
    counter++;

    if(counter < ARRAY_SIZE)
    {   
        functionLoop(example_strings, outputFile, counter);      
    }
}

Thank you to everyone that helped.

Upvotes: 0

Views: 166

Answers (4)

molbdnilo
molbdnilo

Reputation: 66371

Pass the array as a parameter to your function, i.e.

printLoop(outputFile, example_strings, current_index, max_index);

Upvotes: 0

Microkernel
Microkernel

Reputation: 1417

Here are the bunch of problems I can see in the code.

1) Header file is included with inside the function, so if your header file has some variables/arrays declared in it, (I guess your example_strings is in that header), it will become local variable instance for your function, and will take up stack space. And as you recursion continues, it would soon causes Stack OverFlow.

2) Why are you opening file "output.txt" on every recursive call? So, on each call you are opening same fail again and again. Move it out to somewhere to open it only once.

So, here are my suggestions (brief): 1) Move the header #include "exstr.h" out of your function.

2) Don't open same file on each recursive call.

  • Microkernel

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

Try:

void functionLoop(int loopInt) 
{ 
#ifdef ___EXSTR_H___ 
#undef ___EXSTR_H___ 
#endif 
#include "exstr.h" 

    ofstream fout; 
    fout.open("output.txt"); 

    int arrayLength =  sizeof ( example_strings ) / 4; // arrayLength = 5000. 
    while (loopInt < arrayLength)
    {
      char *stringArray = example_strings[loopInt]; 
      int charCount = strlen(stringArray);
      stringArray += charCount;
      cout << loopInt + 1 << ": " << charCount << ": " << example_strings[loopInt] << endl; 
      loopInt++; 
    } 
} 

Upvotes: 0

Adrian
Adrian

Reputation: 5681

if it is bc you ran out of stack due to recursion, remove the recursive call and use an iterative approach. Its very easy to change your code to do so. make a while/for loop over loopInt and check the length of the string at array[loopInt].

Tip
you can use strlen to find the length of a string, you dont need to do it by hand with that loop: while( *stringArray != 0 )

pseudocode:

i=loopInt
while i<arrayLength {
    print strlen(exampleString[i], exampleString[i])
}

Please edit my post. im on a tablet.

Upvotes: 0

Related Questions