Getting Core dump

I am trying to execute the following code, am getting coredump,can one please help on this

#include <iostream>

using namespace std;

static int fun1(int);
static int fun2(int);
static int y;

int main()
{
cout << "Hello World" << endl;

int x=5,y=10,counter;

for(counter=1;counter<=2;counter++)
{
y=y+(fun1(x)+fun2(x));

cout<<"Final values is:"<<y<<endl;
}

return 0;
}

int fun1(int i)
{
y=y+fun2(i);

return y;
}
int fun2(int j)
{
y=y+fun2(j);

return y;
}
Captain Kirk is dumping the core to try to escape from the collapsing black hole. ;D

Hmmm... I'm not exactly sure what causes a core dump, but your function called fun2() looks a little suspicious. I don't know if it was a typo or if you meant for it to be this way, but fun2() is executing recursively (meaning it is calling itself). This is not illegal in C++, but as there doesn't seem to be a way for your function to ever end it will just keep getting deeper and deeper into the recursion.

That might cause the Enterprise to lose it's warp core. :)
Hi Tresky,

You are correct, fun2() is the problem.
I have changed to like this

int fun2(int j)
{
y=y+1;

return y;
}

now its working fine.

Thanks for your quick reply.
Fantastic! You're very welcome.

Have fun. Cheers!
Thanks....
Topic archived. No new replies allowed.