C tail call optimization

后端 未结 8 817
陌清茗
陌清茗 2021-01-31 16:25

I often hear people say that C doesn\'t perform tail call elimination. Even though it\'s not guaranteed by the standard, isn\'t it performed in practice by any decent implement

8条回答
  •  日久生厌
    2021-01-31 17:15

    For those who like proof by construction, here is godbolt doing a nice tail call optimisation and inline: https://godbolt.org/z/DMleUN

    However, if you crank the optimization to -O3 (or no doubt if you wait a few years or use a different compiler), the optimisation totally removes the loop/recursion.

    Here is an example that optimizes down to a single instruction even with -O2: https://godbolt.org/z/CNzWex

提交回复
热议问题