The following program will cause fun 2 ^ (MAXD + 1) times. The maximum recursion depth should never exceed MAXD, though (if my thinking is correct). Thus, it may take some time to compile, but it should not have my RAM.
#include<iostream> const int MAXD = 20; constexpr int fun(int x, int depth=0){ return depth == MAXD ? x : fun(fun(x + 1, depth + 1) + 1, depth + 1); } int main(){ constexpr int i = fun(1); std::cout << i << std::endl; }
The problem is that there is my RAM - this is exactly what it does. When I transfer MAXD to 30, my laptop starts replacing after GCC 4.7.2 quickly allocates 3 GB or so. I have not tried this with clang 3.1 since I do not have access to it right now.
My only assumption is that it has something to do with the fact that GCC is trying to be too smart and memoize function calls, as it happens with templates. If so, doesnโt it seem strange that they have no limit to how many memos they do, for example, the size of the MRU cache table or something else? I did not find a switch to turn it off.
Why should I do this? I play with the idea of โโcreating an advanced compile-time library such as genetic programming or something like that. Since compilers do not have optimized compilation tail processing time, I worry that all that loops will require recursion and (even if I go back to the maximum recursion depth parameter, which seems a little ugly to require) will quickly allocate all of my RAM and it will populate with meaningless stack frames. So I came up with the above solution to get arbitrarily many function calls without a deep stack. Such a function can be used for bending / looping or springboard.
EDIT: Now I tried this in clang 3.1 and it will not leak memory at all, no matter how long I make it work (that is, how high I do MAXD). CPU usage is almost 100%, and memory usage is almost 0%, as expected. Perhaps this is just a bug in GCC.
Gurgeh
source share