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")