Are all functions in C/C++ assumed to return?

后端 未结 7 1538
抹茶落季
抹茶落季 2020-12-31 01:49

I was reading this paper on undefined behaviour and one of the example \"optimisations\" looks highly dubious:

if (arg2 == 0)
    ereport(ERROR,         


        
7条回答
  •  悲&欢浪女
    2020-12-31 02:27

    In embedded systems, functions that never return are commonplace. They should not be optimized either.

    For example, a common algorithm is to have a forever loop in main() (a.k.a. the background loop), and all functionality takes place in an ISR (Interrupt Service Routine).

    Another example are RTOS tasks. In our embedded system project, we have tasks that are in an infinte loop: Pend on message queue, process message, repeat. They will do this for the life of the project.

    Some embedded systems have safe shutdown loops where they place the machine into a safe state, locking out all User Input, and wait for power shutdown or reset.

    Also, some embedded systems can shutdown the system. Shutting down the power prevents the system from returning.

    There are reasons that not all functions need to return or must be required to return. If all functions returned that are in your cell phone, you wouldn't be fast enough to use it.

提交回复
热议问题