ProgrammingHard

A function computes the sum of 1 to n by recursion with depth n. For n = 10^6, what is the main risk?

  1. A. The sum overflows 64-bit integers
  2. B. The function returns a pointer to a local
  3. C. The recursion depth can overflow the call stack, so an iterative loop or a formula is safer
  4. D. The recursion is too shallow to be correct

Correct answer: C. The recursion depth can overflow the call stack, so an iterative loop or a formula is safer

A million nested calls use a lot of stack space, which may overflow. A loop or the closed form n(n+1)/2 avoids the depth.

More C Programming questions

Exams that ask this topic