0

I am trying to improve my C++ and am trying this example

#include <iostream>
void go(int & x){
   x+=1;
   std::cout << x<< std::endl;
   go(x);
}
int main()
{
   int x = 0;
   go(x);
}

but it crashes after a few seconds with this error

261802
261803
261804
261805
261806
261807
261808
261809
261810
[1]    18523 segmentation fault (core dumped) 

using while loops works but self calling function crash

#include <iostream>
int main()
{
   int x = 0;
   while (true)
   {
      x++;
      std::cout << x << std::endl;
   }
}

tried using pointers but having the same crash... looks like its the recusive function not the intger.

    #include <iostream>
void go(int *x){
   *x += 1;
   std::cout << *x<< std::endl;
   
   if(*x==10000000)
      return;
   else
      go(x);
}
int main()
{
   int *x = new int(0);
   go(x);
}

please any one has an idea how to fix this code ? Thanks

SOLVED

Thanks all i just needed to enable compiler optimizations

set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
Amr Osman
  • 119
  • 2
  • 10
  • 7
    Your stack only has so much memory. Your program is infinitely adding to the stack, so you're causing a stack overflow. – Loocid Jun 23 '21 at 03:05
  • 7
    You need a condition to end recursion – nhatnq Jun 23 '21 at 03:06
  • What do you want to happen. Currently it's an endless loop that only terminates because it runs out of memory (each call to go consumes a small amount of memory and stack resources). If you just want to endlessly print numbers you could use a loop instead. – CraigR Jun 23 '21 at 03:08
  • I just want to see the flipping point when integer reaches it max possible value – Amr Osman Jun 23 '21 at 03:10
  • Learning what a base exit case is for a recursive function is the best way to fix this. Just look at your function and ask yourself, *"In what circumstance will this not recurse?"* Until you have a concrete answer to that, the function is broken from inception. – WhozCraig Jun 23 '21 at 03:10
  • 1
    ***I just want to see the flipping point when integer reaches it max possible value*** You will not be able to have it recurse that deep. Your stack is limited to a few MB not GBs – drescherjm Jun 23 '21 at 03:13
  • 1
    @AmrOsman If you merely want to see an int overflow, assign an int to INT_MAX and increase it. I recommend stepping through a debugger like gdb if you're just trying to learn. – Rafael Jun 23 '21 at 03:13
  • I think a found the issue. recalling the same function again and again causes the stack to fill up .. may be function recursion is not effecient at all – Amr Osman Jun 23 '21 at 03:15
  • 2
    If you compile the program with enabled optimisation, the tail recursion optimisation will be applied and you will not get stack overflow, just endless loop. – 273K Jun 23 '21 at 03:24
  • Thanks that fixed it :) adding this lines to cmake did make it work set(CMAKE_CXX_FLAGS "-Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-O2") – Amr Osman Jun 23 '21 at 03:25
  • Related to the tail recursion optimization where the compiler turns your recursive algorithm into a loop removing the recursion and stack requirement: [https://stackoverflow.com/questions/2693683/tail-recursion-in-c](https://stackoverflow.com/questions/2693683/tail-recursion-in-c) – drescherjm Jun 23 '21 at 03:32
  • The function is infinitely recursive. To prevent that, there needs to be an end condition that, when met, causes the function to return to the caller, rather than calling itself again – Peter Jun 23 '21 at 05:47
  • An infinite recursion or loop is a bug. – prehistoricpenguin Jun 23 '21 at 07:42

0 Answers0