My program runs out of memory each time i run a for loop?

Jan 25, 2013 at 10:49pm
Right. I'm writing a text based RPG in which all of the syntax is not in main, neither is the actual calculation of such correct syntax. So, each time I run it and it runs the for loop that checks the array for the actual command and the word adjacent and then returns the value with a sentence. There is an array for the command and for the general template for the command to return. here's the code, can anybody please tell me why it keeps running out of memory? Thanks in advance :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Prerequisites.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cmath>
#include <string>

string input;
string syntaxList[20] = {"look"};
string syntaxSay[20] = {"you look at the"};
bool readyToGo = false;



string inputYey(string& a) {
	cout << "Type something dammet. No more than 20 chars! Or actually write something. Jeez." << endl;
	getline(cin, a);
	while(a.length() > 20 || a.length() <= 0){
		cin.clear();
		cin.sync();
		cout << "Listen to me -_-!" << endl;
		getline(cin, a);
	}
	return a;
}
string syntax(string& a) {
	
	getline(cin, a);
	int c = a.find(" ");
	
	bool done = false;
	for(int i = 1; i++; i < 20 && done != true){
		string d (syntaxList[i], 0, c);
		string e (syntaxList[i], c);
		if(d == a){
			done = true;
			return (syntaxSay[i] + e);
		}else{
			done = false;
		}
	}

	return "Syntax error.";
}
int randomthing(){
	syntax(input);
	_getch();
	randomthing();
	return 0;
}
int _tmain(int argc, _TCHAR* argv[]) {
	cout << "A'ight!" << endl << "(Any key to continue usually ;) )" << endl;
	_getch();
	cout << "Yeah I don't suit this all that well. RANDOM TEXT BASED RPG FOR DA WIIIN" << endl;
	_getch();
	cout << "Say something man! I feel lonely! D:" << endl;
	cout << inputYey(input) << "! Are you kidding me! You're boring!" << endl;
	_getch();
	cout << "anyway, let's get going. you're in a room." << " do something!" << endl; readyToGo = true;
	randomthing();
}


I can't see what's making it run out of memory. I'm only using conio.h for simple functionality and random other things included. Any explanations? It is yet a mystery to be solved on my end.
Jan 25, 2013 at 11:14pm
Maybe I'm missing something... but how is randomthing() ever supposed to end? It calls itself which calls itself which calls itself which calls itself...forever.
Jan 25, 2013 at 11:16pm
Well, freddy, that's the point. I want it so after each action you should be able to type something else in unless I want the program to be terminated, when I return the actual value and the program ends. It's called recursion.
Last edited on Jan 25, 2013 at 11:19pm
Jan 25, 2013 at 11:23pm
...I'm aware of what recursion is, but a pivotal point of recursion is a test for whether the function should be called again, or if it should start the long chain of returns. I just don't see anything here that would stop the function from calling itself forever.
Jan 25, 2013 at 11:24pm
Well, yes, I never got around to that :D
Jan 25, 2013 at 11:26pm
Unfortunately, randomthing() cannot return. Ever.

Your compiler may even warn you that the return statement is unreachable. And, unfortunately, each function call consumes memory, so if you call it enough, you're going to run out of memory.

I'm not sure why you'd want to use recursion for something as simple as:

1
2
3
4
5
6
7
8
9
int randomthing()
{
    for ( ; ; )
    {
        syntax(input) ;
        _getch() ;
    }
    return 0 ;
}


Notice that the function is completely equivalent to your recursive version, including the incapability of returning to the calling function.
Last edited on Jan 25, 2013 at 11:26pm
Jan 25, 2013 at 11:30pm
So... you're aware that the function will never stop calling itself, and you're still asking why it runs out of space? Seems like you already had the answer.
Jan 26, 2013 at 10:00am
It seems painfully obvious now. My skull was hurting last night and I did not really think the problem through. Thankyou for the help guys.
Also, because after each action taken, I want there to be a new user input so there is a continual motion to the text based RPG. I need it to recur otherwise I have to continually call the function over and over. I don't know how many inputs I'm going to get, so there we go. So when I recur what I am doing is I am making sure there is never a point after the function is called that you are not reading or giving an input, but I never got round to actually making a point in which it stops. Well yes, my retard went to the max...
however, I am still having a huge issue with this.
If I use _getch() in the syntax function and it recurs there is no issue.
If I just use the single 1 iteration for loop it runs out of memory.
Why is this? Also, cire, that would not work, as I have no idea how many iterations I need. Once a certain thing is true, the recursion stops. Never got round to it, however.

EDIT: I misread that. I thought the 'for( ; ; ){ }' was a for loop that was using a generalization for conditions. I am feeling rather stupid now.

Update: I have edited it slightly, here's what I've done :P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Prerequisites.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cmath>
#include <string>

string input;
string syntaxList[20] = {"look", "suicide"};
string syntaxSay[20] = {"you look at the", "you die."};
bool endGame[20] = {false, true};
bool readyToGo = false;
bool gameOver = false;



string inputYey(string& a) {
	cout << "Type something dammet. No more than 20 chars! Or actually write something. Jeez." << endl;
	getline(cin, a);
	while(a.length() > 20 || a.length() <= 0){
		cin.clear();
		cin.sync();
		cout << "Listen to me -_-!" << endl;
		getline(cin, a);
	}
	return a;
}
string syntax(string& a) {
	
	getline(cin, a);
	int c = a.find(" ");
	
	bool done = false;
	for(int i = 1; i++; i < 20 && done != true){
		string d (syntaxList[i], 0, c);
		string e (syntaxList[i], c);
		if(d == a){
			done = true;
			if(endGame[i] = true){
				gameOver = true;
			}
			return (syntaxSay[i] + e);
		}else{
			done = false;
		}
	}

	return "Syntax error.";
}
int randomthing(){
	syntax(input);
	_getch();
	if(gameOver == false){
		randomthing();
	} else {
		return 0;
	}
}
int _tmain(int argc, _TCHAR* argv[]) {
	cout << "A'ight!" << endl << "(Any key to continue usually ;) )" << endl;
	_getch();
	cout << "Yeah I don't suit this all that well. RANDOM TEXT BASED RPG FOR DA WIIIN" << endl;
	_getch();
	cout << "Say something man! I feel lonely! D:" << endl;
	cout << inputYey(input) << "! Are you kidding me! You're boring!" << endl;
	_getch();
	cout << "anyway, let's get going. you're in a room." << " do something!" << endl; readyToGo = true;
	randomthing();
}

Notably: no change in the problem.

Another Edit: I'm too stupid for this. Sorry for wasting your time ^_^. I realise the problem.

Another EDIT: Yeah, I fixed that, but it's definitely the for loop now. I realise this after alot of debugs. I can't see the problem with it now.
Last edited on Jan 26, 2013 at 10:20am
Jan 26, 2013 at 2:49pm
for(int i = 1; i++; i < 20 && done != true){

for ( init; condition; update ) not for (init ; update; condition)

Also, randomThing is still a poor candidate for recursion as well as being incorrect (only 1 call to randomThing would actually result in a value being returned.)

1
2
3
4
5
6
7
8
int randomthing(){
    while ( !gameOver )
    {
        syntax(input);
	_getch();
     }
     return 0 ;
}
Last edited on Jan 26, 2013 at 2:50pm
Jan 26, 2013 at 4:11pm
That is better.
I am so very braindead.
Thankyou cire, you've been very helpful.
I took a couple of months break and some things turned a little blurry on my end. You've both been quite helpful.
Topic archived. No new replies allowed.