non-void function works fine even without executing return

前端 未结 2 433
名媛妹妹
名媛妹妹 2021-01-22 19:00

I\'m a computer science college student. Yesterday, I have a class about Binary Search Tree using C++. We are taught by lab assistants in that class.

They define the nod

2条回答
  •  野性不改
    2021-01-22 19:49

    It's just undefined behavior.

    It appears to work because there's one register that holds the return value. In the deepest paths of the recursion tree, the temp is moved into that register, and never cleared or changed afterwards, so that register will contain the correct node until the first call returns.

    When the first call returns, the calling context checks that register for the return value, which happens to be correct. But you shouldn't rely on this. It's not safe to assume it will always work.

提交回复
热议问题