首页IT科技assessment词根(assert C++ Reference)

assessment词根(assert C++ Reference)

时间2025-05-01 01:46:59分类IT科技浏览4178
导读:assert...

assert

void assert (int expression);
Evaluate assertion

If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false), a message is written to the standard error device and abort is called, terminating the program execution.

The specifics of the message shown depend on the specific implementation in the compiler, but it shall include: the expression whose assertion failed, the name of the source file, and the line number where it happened. A usual expression format is:

Assertion failed: expression, file filename, line line number

This macro is disabled if at the moment of including assert.h a macro with the name NDEBUG has already been defined. This allows for a coder to include many assert calls in a source code while debugging the program and then disable all of them for the production version by simply including a line like:

#define NDEBUG at the beginning of its code, before the inclusion of assert.h.

Therefore, this macro is designed to capture programming errors, not user or running errors, since it is generally disabled after a program exits its debugging phase.

Parameters

expression Expression to be evaluated. If this expression evaluates to 0, this causes an assertion failure that terminates the program.

Return Value

none

Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* assert example */ #include <stdio.h> #include <assert.h> void print_number(int* myInt) { assert (myInt!=NULL); printf ("%d\n",*myInt); } int main () { int a=10; int * b = NULL; int * c = NULL; b=&a; print_number (b); print_number (c); return 0; }

In this example, assert is used to abort the program execution if print_number is called with a null pointer as attribute. This happens on the second call to the function, which triggers an assertion failure to signal the bug.

声明:本站所有文章            ,如无特殊说明或标注                  ,均为本站原创发布            。任何个人或组织      ,在未征得本站同意时            ,禁止复制            、盗用                  、采集      、发布本站内容到任何网站      、书籍等各类媒体平台                  。如若本站内容侵犯了原著者的合法权益                  ,可联系我们进行处理      。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
nginx负载均衡(flask,uwsgi,nginx部署配置) 免费wordpress博客主题(免费WordPress——开启个人博客的最佳选择)