递归调用溢出(recursive call overflows)

编程入门 行业动态 更新时间:2024-10-23 00:21:57
递归调用溢出(recursive call overflows)

在测试数据集上,下面的代码可以工作,但是当我更改为第二个具有相似大小的测试集时,它会溢出。

要将一串标记转换为一个相关的新标记串,我使用这个矢量查找功能

//looks for input string in vector and returns output, 'c' is check row, 'r' is return row string vectorSearch(string &check, int &direction, int n, int c, int r, int level) { if ((direction == 1 && check.length() <= 1) || n == list.size()-1 ||(direction == 0 && check.length() > 1)) { //if reading and string is 1 char then pass over if (direction == 1){ //convert '???' into '?' string temp = ""; bool wildToken = false; for (unsigned int i = 0; i < check.length(); i++) { temp+='?'; if (check.compare(temp) == 0) { check = '?'; wildToken = false; } //done,'???" case, return '?' token else if (check[i] == '?') wildToken = true; //not done searching } } return check; } else { if (list[n][c] == check || list[n][c] == ('0'+check)) //add dummy '0' return list[n][r]; else return vectorSearch (check, direction, n+1, c, r, level); } }

在为十几次转换工作正常后,堆栈溢出

vectorSearch从这个函数被调用

//this function takes an ontology and direction==1 (default) changes from string //to single char or if direction==0 takes single char and converts to string representation string Lexicon::convertOntology(string input, int level, int direction, string out, string temp) { if (input == "" && temp == "") return out; //check for completed conversion else { if (direction == 0 || input[0] == '.' || input[0] == '-' || input == "" ) { //found deliniator or end if (temp == "") temp = input[0]; //condition for reverse w/o deleniators if (input != "") return convertOntology(input.substr(1), level+1, direction, out+=vectorSearch(temp, direction, 0, direction, 1-direction, level)); else { string empty = ""; return convertOntology(empty, level+1, direction, out+=vectorSearch(temp, direction, 0, direction, 1-direction, level)); } } else return convertOntology(input.substr(1), level, direction, out, temp+=input[0]); //increment and check } }

On a test data set the following code works, but when I change to a second test set with a similar size it overflows.

To change a string of tokens into an associated new string of tokens I use this vector lookup function

//looks for input string in vector and returns output, 'c' is check row, 'r' is return row string vectorSearch(string &check, int &direction, int n, int c, int r, int level) { if ((direction == 1 && check.length() <= 1) || n == list.size()-1 ||(direction == 0 && check.length() > 1)) { //if reading and string is 1 char then pass over if (direction == 1){ //convert '???' into '?' string temp = ""; bool wildToken = false; for (unsigned int i = 0; i < check.length(); i++) { temp+='?'; if (check.compare(temp) == 0) { check = '?'; wildToken = false; } //done,'???" case, return '?' token else if (check[i] == '?') wildToken = true; //not done searching } } return check; } else { if (list[n][c] == check || list[n][c] == ('0'+check)) //add dummy '0' return list[n][r]; else return vectorSearch (check, direction, n+1, c, r, level); } }

After working fine for a dozen conversions the stack overflows

vectorSearch is called from this function

//this function takes an ontology and direction==1 (default) changes from string //to single char or if direction==0 takes single char and converts to string representation string Lexicon::convertOntology(string input, int level, int direction, string out, string temp) { if (input == "" && temp == "") return out; //check for completed conversion else { if (direction == 0 || input[0] == '.' || input[0] == '-' || input == "" ) { //found deliniator or end if (temp == "") temp = input[0]; //condition for reverse w/o deleniators if (input != "") return convertOntology(input.substr(1), level+1, direction, out+=vectorSearch(temp, direction, 0, direction, 1-direction, level)); else { string empty = ""; return convertOntology(empty, level+1, direction, out+=vectorSearch(temp, direction, 0, direction, 1-direction, level)); } } else return convertOntology(input.substr(1), level, direction, out, temp+=input[0]); //increment and check } }

最满意答案

调用堆栈是一个有限的资源,可以像其他任何其他资源一样耗尽。 你的函数越大(相对于你在其中创建的局部变量的创建而言),每个调用在堆栈上使用的空间量越大。 除非你能以某种方式限制递归调用的次数,否则这是递归不可避免的。

thank you to the reviews and comments.

The functions are fine - this recursive function bundle requires that the string exists in the database it acts an, and the string checks prior to these incorrectly recognized a special condition and inserted a dummy char. There is the recursive function that precedes these two - I did not correctly see that I had written a bundle of three recursive functions - and that one was searching within parameters for a string longer than what exists in the database; apparently the parameters were wider than the stack. Checked into the parameters and one was not updated and was not controlling.

I fixed the special condition, the strings are now the same length and the search parameters are fixed.

the functions posted are not too complex.

更多推荐

本文发布于:2023-04-29 07:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   recursive   overflows   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!