Preventing Compiler from Crashing

I'm using Visual Studio to program and I want to run a program that has to check about 100 million possibilities and the compiler keep crashing after 15 seconds. I have run longer programs time wise on the computer before but I don't know why it keeps crashing.

I'm using recursion to try to cut down on compiling time instead of a for loop

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
  #include <iostream>
using namespace std;

int comparison(int count); //prototype
int recurLoop(int loop); //prototype

int main()
{
	int meaningless = 0;
	cout << "Enter any number to begin: ";
	cin >> meaningless;

	meaningless = recurLoop(1);
	cout << "The average amount of times to get the correct number for 99.99% is one in: " <<  meaningless << endl;
	return 0;
}

int recurLoop(int loop) {
	int avg = 0;
	if (loop > 10000)
		return (avg / loop);
	else
	{
		avg += comparison(0);
		recurLoop(loop++);
	}
}



int comparison(int count) {
	int x = rand() % 10000;
	int y = rand() % 10000;
	//cout << x << " , " << y<<endl;
	
	if (x == y) 
	{
		cout << count << endl;
		return count;
	}
	else
		comparison(count++);
}


Is there any way I can run this program using the normal cmd compiler or do I need a different one. I have an i7 computer with 16gb of RAM so there shouldn't be an issue with the 'horse power' of the machine.
recursion pushes on the stack each call and drains your memory which causes a crash. Recursion is generally slower an a big problem for routines that need a LOT of recursive calls. There is a well known do-nothing program to blow up your machine called ackermans' function. Even today's machines can't get too far with that one :)

Turn it back into a loop.
While you are at it, multi-thread it so you can do it 4x or better faster depending on how many cores your machine has.

You probably are not using all 16gb. The limit is the STACK in the memory model your program has. I think visual will let you make this value much bigger, but I don't know if its enough to make 100M recursive calls.

For reference, lets say it takes, I dunno, 200 bytes (couple of 64 bit pointers, storage for your parameter, and a little under the hood stuff) to push on the stack. 100M * 200 is ~19,000 MB of stack space.
Last edited on
I think line 25 should be
loop++;
Thank you, it works now. My professor told us that recursion was faster for certain applications. I guess its not the case in this example.

@SamuelAdams - that would break the recursion and will run the random number comparison once but i want it to continue checking numbers for 10,000 times.
Last edited on
Recursion is never faster, actually. Its easier to write (much, much easier in many cases!!), but I can't think of anyplace where it is faster to make repeated function calls (which are a bit expensive at the OS level) vs a loop (which is cheap, and optimized at the chip level to use a counter register, etc).

Even ye olde quicksort, long hailed as a champ, is often in practice sent off to shell sort after a few recursive passes to preorder it.


As people mention a stack overflow might occur. You can still have zero cost recursive function if you can get them constepr properly such as Fibonacci. Think of using algorithm as well and appropriate data structure.
> My professor told us that recursion was faster for certain applications.

Yes. Tail call recursion is measurably faster in many functional programming languages (for instance Scheme) where mutating operations on objects are expensive.

In C++, this is rarely the case; modern compilers tend to produce heavily optimised tail recursive calls, but even so, at best recursion would be only marginally faster than iteration.

EDIT: the code had a silly bug, deleted


The code posted originally engenders undefined behaviour;
both int recurLoop(int loop) and int comparison(int count) do not return values if the else is hit.
Last edited on
> recurLoop(loop++);
C++; // make C bigger, return old value
you were recursing over and over again with the same value.
make your intentions clearer recurLoop(loop+1);

Same error in `comparison()'


Apart, learn the terminology: your program was crashing, not the compiler.
The compiler simply translates the source code that you've written into an executable.

Also, learn to use the debugger. A simple look at the call stack would have pointed your mistake.
Topic archived. No new replies allowed.