I was wondering if there is a way to make an infinity loop the first function will call the second and the second will call the first and so on. Please make a code in C++. Thanks already.
Asked
Active
Viewed 345 times
-12
-
4That sounds like a bad idea, why? – Shaun Bebbers Apr 04 '17 at 13:37
-
It is possible. The main question is will you be able to have `tail call optimization` on both functions? Otherwise, it will not be infinite. Good luck. – stefaanv Apr 04 '17 at 13:41
-
It's maybe meaningful question in "stackoverflow" – suhdonghwi Apr 04 '17 at 13:41
-
I am interested in why? Also why do you need to two void functions? – CodeCupboard Apr 04 '17 at 13:55
1 Answers
3
void foo();
void bar();
void foo(){
bar();
}
void bar(){
foo();
}
int main() {
foo();
return 1;
}
Not sure what you're trying to achieve here...? except.. StackOverflow.. ohhh!

AlexG
- 1,091
- 7
- 15
-
1You beat me to it ;-) except that you will need a main entry point? – Shaun Bebbers Apr 04 '17 at 13:40
-
1@ShaunBebbers To be honest I'd rather not give any entry point for such a degenerate atrocity, but updated anyway :) – AlexG Apr 04 '17 at 13:43
-
Does not really work, clang and icc compile this to `int main () {}`. https://godbolt.org/g/7PRTov – Baum mit Augen Apr 04 '17 at 13:45
-
3@BaummitAugen right. VS2013 does only compile it in debug, and warns me I'm an idiot in release. :) – AlexG Apr 04 '17 at 13:47
-
Nobody said the functions had to be empty. Touch a volatile or something. – François Andrieux Apr 04 '17 at 13:50
-
@AlexG - you need a return value on the `int main()` function so that it grac... oh wait! – Shaun Bebbers Apr 04 '17 at 13:50
-
2@ShaunBebbers No you don't, even when the program does terminate. – Baum mit Augen Apr 04 '17 at 13:50
-
Hm, how to define "infinity"??? According to my definitiion, this solution surely is not infinite, unless one provides an architecture with infinite memory (oh: where to buy such one? I might be interested in...). On the other hand: a simple `for(;;);` won't run infinitely either as at some point far in the future, hardware will certainly fail due to obsolescence... – Aconcagua Apr 04 '17 at 13:56
-
-
Couldn't one also use a `do...while()` or a simples `while()` loop to achieve more or less the same result? – Shaun Bebbers Apr 04 '17 at 14:01
-
1@Aconcagua Infinite memory isn't required if the compiler does tail-call optimization! – cdhowie Apr 04 '17 at 14:35
-
-