[Bug c++/85890] New: cc1plus runs out of memory in recursive Fibonacci computation

janniksilvanus at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed May 23 14:31:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85890

            Bug ID: 85890
           Summary: cc1plus runs out of memory in recursive Fibonacci
                    computation
           Product: gcc
           Version: 7.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janniksilvanus at gmail dot com
  Target Milestone: ---

~/tmp: g++ --version
g++ (SUSE Linux) 7.3.1 20180323 [gcc-7-branch revision 258812]
[...]

~/tmp: cat fibonacci.ii 
constexpr int fib(int n)
{
    return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

int foo() {
   int const result = fib(45);
   return result;
}
~/tmp: g++ fibonacci.ii -c
<runs forever until out of memory>

- Version 7.3.0 seems to be affected as well, but not versions 7.2.1 and 8.1.0.
- Omitting the 'const' in 'int const result', or replacing by 'constexpr' fixes
the problem.
- Using smaller values instead of 45 (e.g. 32), it seems the runtime of gcc is
roughly proportional to the value of the computed result, indicating that gcc
is recursively (without memoization!) computing the value, which has
exponential runtime.


More information about the Gcc-bugs mailing list