When should we use asserts in C?

前端 未结 9 1737
孤街浪徒
孤街浪徒 2020-12-02 11:21

I am writing a function in C. As a matter of style, when is it good to use assert compared to returning an error code. Lets say the function is dividing two numbers. Should

9条回答
  •  伪装坚强ぢ
    2020-12-02 12:12

    Use an assert when your program meets a situation that does not allow to continue. Assertions are a 'contract' and I use them as a 'contract' with OS and situation 'danger in delay'.

    In order to emulate exceptions you can still use GOTO 'ERRORLABEL' and terminate clean after running a clean up function.

提交回复
热议问题