When building, one message .... windows has stopped!!

When building, one message .... windows has stopped!!
Any ideas??
One thing else...there was another problem before this problem ... overload stack ...PLEASE HELP!
there was another problem before this problem ... overload stack

If there was stack overflow it's likely the last problem and not the one before last.

Please show the code that produces this, and a stack trace also to reduce amount of code you need to paste.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int pow (int t,int s);
int t =5, s=3;
int main ()
{
	
	int a=5,b=3;
	cout<<pow(a,b)<<endl;
return 0;
}

int pow (int t, int s)
{
	if (t>=1)	
		return t * pow(t,s-1);
	else
	return 1;
}

when I try to build it windows stops, when I try to single step it, it shows me unhandled stack overflow.
This is output message:
'UNIQUE.exe': Loaded 'E:\my project\UNIQUE\Debug\UNIQUE.exe', Symbols loaded.
'UNIQUE.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'UNIQUE.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'UNIQUE.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'UNIQUE.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'UNIQUE.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x011448a9 in UNIQUE.exe: 0xC00000FD: Stack overflow.
Unhandled exception at 0x011448a9 in UNIQUE.exe: 0xC00000FD: Stack overflow.
(1.) Line 7: You call pow w/ t = 5.
(2.) Line 14: You recursively call pow w/ t = 5. Go to (1.)

You have a recursive infinite loop, which inevitably leads to a stack overflow.

In other words, your terminating condition for recursion is wrong and never happens.
Last edited on
Ganado, it seems you're right as usual!
it's if (s>=1)
not t.
But what I don't understand is this formula:
 
 t * pow(t,s-1);

how could we mult. number by number & number?? and why 't' chose to multiplied by itself and ignored 's-1'??
Thanks in advanced!
Last edited on
For you power function to work you also need to handle the case where t < 1
not just t >= 1, that's why you get stack overflow.

the t < 1 check is valid check only if s is positive decimal number.

There is a similar post about power function where I posted the formulas and other people posted partial solution, take a look here:

http://www.cplusplus.com/forum/beginner/270016/
Last edited on
All what i'm asking about is why n doesn't multiply also by b-1.
Last edited on
All what i'm asking about is why n doesn't multiply also by b-1.

It's not clean what you're asking, if by b you mean "base" then that's wrong because base must never be modified.
Last edited on
Dear brother, malibor, yes now I understood what you meant, thanks a lot for your attention!
Topic archived. No new replies allowed.