Is assert(false) ignored in release mode?

隐身守侯 提交于 2019-11-27 21:34:37

问题


I am using VC++. Is assert(false) ignored in release mode?


回答1:


If compiling in release mode includes defining NDEBUG, then yes.

See assert (CRT)




回答2:


The assert macro (at least it is typically a macro) is usually defined to no-op in release code. It will only trigger in debug code. Having said that. I have worked at places which defined their own assert macro, and it triggered in both debug and release mode.

I was taught to use asserts for condition which can "never" be false, such as the pre-conditions for a function.




回答3:


IIRC, assert(x) is a macro that evaluates to nothing when NDEBUG is defined, which is the standard for Release builds in Visual Studio.




回答4:


Only if NDEBUG is defined I think (which it will be by default for Visual C++ apps).




回答5:


I think it is a mistake to rely too much on the exact behavior of the assert. The correct semantics of "assert(expr)" are:

  • The expression expr may or may not be evaluated.
  • If expr is true, execution continues normally.
  • If expr is false, what happens is undefined.

More at http://nedbatchelder.com/text/assert.html




回答6:


same for GNU :

  #ifdef    NDEBUG

  # define assert(expr)     (__ASSERT_VOID_CAST (0))


来源:https://stackoverflow.com/questions/270488/is-assertfalse-ignored-in-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!