recursion - Recursive function behavior in c++ -
i know basic definition of recursive function.....but want know impact on memory?? why not chosen on loop or iteration?
c++ not mandate tail call optimization, recursive functions can trivially converted loops may still take space linear in call depth (storing frame).
additionally, c/c++ not necessarily detect stack overflow, that's problem potentially deep calls.
edited have more qualified language ("necessarily")
edit
some people seem hung on fact specific compilers such gcc , clang perform tail call optimization and/or stack overflow detection. point is, if not mandated, unsafe. example, gcc performs tail call optimization @ -o2, -o3 , -os. so, if program depends on tail call optimization being performed, program mysteriously die when compiles without optimization flag (on gcc).
if programming practice, then, means, can continue write programs depend on compiler flags. or ones depend on compilers. optimization not optional.
Comments
Post a Comment