tags:

views:

123

answers:

4

Possible Duplicate:
Recursive main() - why does it segfault?

if im calling recursive function without any parameter then stackoverflow error occur so what is the reason behind this

+7  A: 

This is implementation-specific, but is used on numerous platforms. When a function needs to return the processor needs to know where exactly it was called from - so-called return address.

That return address needs to be stored prior to calling the function. In many implementations the program stack is used for storing the return addresses as well as for storing the parameters. So even when there're no parameters to the function program stack is still used and too deep of recursion causes stack overflow.

sharptooth
Which means that your function is called so many times that the return addresses no more fit into the stack (too many calls).
PeterK
plus stack memory the compiler allocates for restoring some registers
Nick D
+5  A: 

No parameter? Then where is your terminating condition?

void infinity()
{
    infinity();
}
Johnsyweb
using globals may be?
Naveen
He could use some global variable or `rand()` or some input device or whatever else.
sharptooth
@SunilRai86: Perhaps you could post a little more code to show whether you are using a global here?
Johnsyweb
+3  A: 

A recursive function with no paramters doesn't make a lot of sense, as you would have no controlling variable with which to terminate the recursion.

anon
He could be using globals, a bad idea to be sure. But not outside the realm of possibility.
Shraptnel
A: 

The value of program counter (PC) or Instruction pointer (PC is a register stores address to next instruction) is pushed into stack just before executing a function and value of PC is changed to first instruction of the function, after finishing the old value (poped out) retained by PC. In infinite recursion the value is pushed again and again. Hence the stack overflow.

Shrikant