3 Replies - 428 Views - Last Post: 04 June 2013 - 08:42 PM Rate Topic: -----

#1 benny850  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 13

Runtime Stack Overflow Error

Posted 04 June 2013 - 07:01 PM

Here is my error message:
Warning 1 warning C4717: 'getx' : recursive on all control paths, function will cause runtime stack overflow

and here is my code I put a comment around the part that is giving me problems!

Can anyone help me please?!?!?!?!

class point
{
public:
point();
point(int xx, int yy);
friend double distance(point p1, point p2);
int getx();
int gety();
double dist;
double xnum, ynum, total;


private:
int x, y;

};

point::point() 
{

x = 1;
y = 1;

}

point::point(int xx, int yy)
{
x = xx;
y = yy;
}

//********************This is the part that is giving me problems************************
double distance(point p1, point p2)
{
double dist;
double xnum, ynum, total;


xnum = (p2.getx() - p1.getx());
xnum = pow (xnum, 2.0);

ynum = (p2.gety() - p1.gety());
ynum = pow (ynum, 2.0);

total = xnum + ynum;

dist = sqrt (total);

return dist;
}

int getx()
{
return getx();
}

int gety()
{
return gety();
//************************************************************************
}



Is This A Good Question/Topic? 0
  • +

Replies To: Runtime Stack Overflow Error

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Runtime Stack Overflow Error

Posted 04 June 2013 - 07:14 PM

int getx(){
   return getx();
}


if the return value for getx() executes getx(), when does the loop end....

Ill give you a short batch code that will give you a better idea of whats going on...

Create this file in notepad and name it getx.bat
:loop
start getx.bat
goto loop


then double click on it.... make sure you save your work first.....

This post has been edited by UG Cyber: 04 June 2013 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Runtime Stack Overflow Error

Posted 04 June 2013 - 07:52 PM

You have infinite recursion (well recursion until you run out of state space). Every recursive algorithm or formula has a base case. A base case ends the recursion and lets the call stack unwind.

i.e.
/* without base case */
int sum(int n) {
   return n + sum(n-1); /* infinite recursion */
}

/* with base case */
int sum(int n) {
   if(n == 0)
      return 0; /* breaks recursion */
   return n + sum(n-1);
}


Was This Post Helpful? 1
  • +
  • -

#4 benny850  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 13

Re: Runtime Stack Overflow Error

Posted 04 June 2013 - 08:42 PM

View Postjjl, on 04 June 2013 - 07:52 PM, said:

You have infinite recursion (well recursion until you run out of state space). Every recursive algorithm or formula has a base case. A base case ends the recursion and lets the call stack unwind.

i.e.
/* without base case */
int sum(int n) {
   return n + sum(n-1); /* infinite recursion */
}

/* with base case */
int sum(int n) {
   if(n == 0)
      return 0; /* breaks recursion */
   return n + sum(n-1);
}



Thanks A lot,
I figured it out! But I'm getting junk as my output now. LOL!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1