non-void function works fine even without executing return

前端 未结 2 406
名媛妹妹
名媛妹妹 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:43

    Typically, you've got one hardware register used to return value from a function (that'd be %EAX on i686, for instance). If the last thing you did in your function is calling another function and the intended return value is the result of that function, it may occur that %EAX value hasn't been trashed and is nicely retrieved by the caller.

    That's subject to many hypothesis, however, and you shouldn't expect it to be reliable in any way. First of all, the compiler can detect that you're not using the return value of search(), so it might decide to optimize away that call if it has hints about search() having no side effects. If it's capable of inlining the search call, it could partly optimize it away.

    There may be other architectures (iirc, that'd be the case with IA64) where the calling convention are more subtle, and where the registers used for returning values in one function are not the same as in the caller function. So your code relies on platform-dependent details to be working while it's supposed to be a high-level (and portable) language.

提交回复
热议问题