c++ - Does this StackOverFlowException require pointers to correctly work? -


i made program searches palindromes reading in .txt file, placing output onto separate .txt file.

it works should 50 or characters. if copy , past large amount of words onto .txt file read, @ runtime "process terminated due stackoverflowexception". opens file called chkstk.asm , has arrow pointing called- "test dword ptr [eax], eax ; probe page". next box comes break , continue option , says, "an unhandled exception of type 'system.stackoverflowexception' occurred in palindrone.exe"

i working on changing things around use pointers , such , possibly store things in vectors. however, still not sure reason error , want know why acting , needs changed can have read , process large blocks of text. not having pointers reason working incorrectly?

#include <iostream> #include <string> #include <ctype.h> #include <iterator> #include <algorithm> #include <fstream>  using namespace std;  /**     recursivly reverses string     @ param word, word being entered     @ last_char, last character in string     @ go, recursive function return character , continue inside **/ string string_reverse(string word) {     if (word.length() - 1 > 0)     {         char last_char = word[word.length()-1];         word.erase(word.length()-1);         string go = string_reverse(word);         return go.insert(0, 1, last_char);     }     else           return word.substr(0,1); }  /** @param in, input original string @param la, reverse string **/ bool equals(string in, string la) {     if(in == la)         return true;     else          return false; }  /** processes pal **/ void process_pal(ofstream &outfile, string in, string la, bool sam) {     if (sam == true)     {         outfile << in << " equal backwards: " << la << "\n";     }     else         outfile << in << " not equal backwards: " << la << "\n"; }  /**     removes numbers, white spaces, , invalid symbols !isalpha     @param sentence, sentence being entered     @ it, iterator iterator through sentence checking invlaid sysmbols **/ string remover(string sentence) {     string::iterator = sentence.begin();      while (it != sentence.end())     {          while( != sentence.end() && !isalpha(*it))          {              = sentence.erase(it);          }          if (it != sentence.end())              ++it;     } return sentence; }  /**     increments find paladrome starting @ 3 0, moving right 1 3 each time util     goes end. once hits end, increment 4 , same thing till     has become full length of text. **/ void find_pal(ofstream &outfile, string input, int pos, int lin) {     int max_length = input.length()+1;     int pos_last = max_length - lin;     if(lin < input.length()){         string sub_fwrd = input.substr(pos,lin);         string sub_bck = string_reverse(sub_fwrd);         bool same = equals(sub_fwrd, sub_bck);         process_pal(outfile, sub_fwrd, sub_bck, same);         pos++;         if (pos == pos_last){             pos = 0;             lin++;         }         find_pal(outfile, input, pos, lin); } }  int main() {     bool con = true;     while (con == true)     {         string input;         ifstream infile;         infile.open ("file_read.txt");             getline(infile,input); // saves lines file in string input.         infile.close();          transform(input.begin(), input.end(), input.begin(), ::tolower); // goes lower case          string inputer = remover(input); // removes unwanted symbols, numbers, spaces, etc         input = inputer; // updates our orignal string input          ofstream outfile ("file_out.txt");         int pos = 0;         int lin = 3;         find_pal(outfile, input, pos, lin); // start palindron function sift       through purmutations          string full_input = string_reverse(input); // final purmutation of reverse         bool same = equals(input, full_input);         process_pal(outfile, input, full_input, same); // final analyzing process_pal          string go;         outfile.close();             cout << "continue? y/n " << endl; // continue on or not         getline(cin, go);         if(go != "y")             con = false;     }     system("pause");     return 0; } 

in c/c++ there stack , heap. stack typically fixed size segment of memory smaller might think (defaults can around 1-2mb) , heap dynamic section of memory grows right on until exceed total logical ram on server.

the short answer question each time nest function call allocate little bit more memory in called "stack frame". if have "main calls calls b calls b again" have 4 stack frames. if have recursive method grows size of input start allocate lot of stack frames , have stack overflow exception (the size of stack has exceeded fixed limit).

so, in general, root cause of problem recursion nesting deeply. there couple of ways of working around this. 1 common way has been mentioned in comments abandon recursion. approach use tail recursion avoids adding new stack frame each call , allows keep recursive semantics.

all being said, should mention might see small benefit if switch pointers. because stack frame's size based on size of function parameters , local variables. pointer might smaller other structure passing in , result in smaller stack frame. however, not root cause of problem.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -