断言失败的vc++文档

In this article, we’ll take a look at using the concept of assertions, using the assert in C/C++.

在本文中,我们将研究使用断言的概念,以及在C / C ++中使用断言。

This is not very hard to understand, so let’s get started!

这不是很难理解的,所以让我们开始吧!

We’ll first look at what we mean by an assertion, and then look at how we can use it in our C programs to debug effectively!

我们将首先了解断言的含义,然后了解如何在C程序中使用它来有效调试!



什么是断言? (What is an assertion?)

An assertion is a specification that verifies that a program satisfies certain conditions at particular points, during its execution (run-time condition checks).

断言是一种规范,用于在程序执行期间(运行时条件检查)验证程序在特定点上满足某些条件。

For computer programs, there are primarily three types of assertions checks:

对于计算机程序,主要有三种断言检查类型:

  • Precondition Assertion -> Condition satisfied before main body execution前提条件断言 ->执行主体之前满足条件
  • Post-condition Assertion -> Condition satisfied after main body execution后置条件声明 ->执行主体后满足条件
  • Invariant Assertion -> Condition satisfied after every repetitive region of a function (like a loop)不变断言 ->在函数的每个重复区域之后都满足条件(例如循环)

Now that we know what an assertion is, let’s look at doing this in a C program.

现在我们知道了什么是断言,让我们来看一下在C程序中执行此操作的过程。



在C程序中使用断言 (Using assertions in C programs)

In C, we use the assert macro to define an assertion statement. This is there in the <assert.h> header file.

在C语言中,我们使用assert宏定义断言语句。 在<assert.h>头文件中。

To define an assertion, we can write something like this:

要定义一个断言,我们可以这样写:


#include <assert.h>assert (condition);

Here, condition must be boolean. For example, the below is an example of an assertion:

在此, condition必须为布尔值。 例如,以下是断言的示例:


int i=0;
assert (i >= 0);

The above assertion holds true, since 0>=0. Therefore, during execution, our program continues normally.

上面的断言成立,因为0>=0 。 因此,在执行过程中,我们的程序可以正常继续。

If the assert condition holds false, it will produce an error, and our program will stop executing, with suitable error messages.

如果断言条件为假,它将产生一个错误,并且我们的程序将停止执行,并显示适当的错误消息。


int i = 0;
// This is false
assert (i > 0);

Now that we’ve covered the basics, let’s look at using assertions in a for loop.

现在我们已经介绍了基础知识,让我们看一下在for循环中使用断言。

This not only ensures that we want the program to do what we want, but also structures code logically, so that it is easy to read.

这不仅可以确保我们希望程序执行所需的操作,而且还可以逻辑地构造代码,以便于阅读。



一个示例–在循环中使用assert (An example – Using assert in a loop)

Consider the below code, that simply adds integers within a given range. We want to ensure that our final result is always positive, and does not overflow.

考虑下面的代码,该代码仅将给定范围内的整数相加。 我们要确保我们的最终结果始终是积极的,并且不会溢出。


#include <stdio.h>
#include <limits.h>
#include <assert.h>int loop_function(int a, int b) {// Precondition assertion (a <= b)assert (a <= b);int result = 0;for (int i=a; i<=b; i++) {// Invariant assertion// The cummulative result must always be positive// Sometimes, the result may overflow and give a negative// integer. In that case, this assertion will failassert (result >= 0);result += i;}// Postcondition assertion// Again, the net result must be positive// So if result = 0, this condition will failassert (result > 0);return result;
}int main() {int a = 3;int b = 10;printf("loop_function on %d and %d gives %d\n", a, b, loop_function(a, b));int c = INT_MAX - 3;int d = INT_MAX;// Here, in case of c and d, the result will overflow. The invariant assertion will be false!printf("loop_function on %d and %d gives %d\n", c, d, loop_function(c, d));return 0;
}

Output

输出量


loop_function on 3 and 10 gives 52
assert_loop: assert_loop.c:14: loop_function: Assertion `result >= 0' failed.
[1]    337 abort (core dumped)  ./assert_loop

If the value of our result is too high (beyond the integer size), then the result will overflow and become negative!

如果结果的值太高(超出整数大小),则结果将溢出并变为负数!

Our second set of input does this, and hence the loop invariant assertion will fail!

我们的第二组输入会执行此操作,因此循环不变断言将失败!

So our program will halt there itself. This ensures that we can detect any design flaw in our program, and handle such inputs accordingly.

因此,我们的程序将自行停止。 这确保了我们可以检测到程序中的任何设计缺陷,并相应地处理此类输入。



结论 (Conclusion)

In this article, we learned how we could check for assertions, using the assert macro in C/C++. For similar articles on C, do look at our tutorial section on C programming.

在本文中,我们学习了如何使用C / C ++中的assert宏来检查断言。 有关C的类似文章,请查看我们有关C编程的教程部分。

参考资料 (References)

  • Berkeley Guide on using assertions in C/C++Berkeley关于在C / C ++中使用断言的指南


翻译自: https://www.journaldev.com/39002/assert-in-c-plus-plus

断言失败的vc++文档

断言失败的vc++文档_了解C / C ++中的断言相关推荐

  1. virtualbox中文技术文档_随笔--西门子STEP7中如何寻找技术文档

    西门子STEP7软件支持的编程语言除了常用的LAD/FBD/STL,还有SCL/GRAPH等,应该说除了LAD/STL之外,SCL和GRAPH也是比较常用的,至少对我个人来说是这样,但是每种指令在不同 ...

  2. java如何打开word文档_如何在Java中打开和操作Word文档/模板?

    我知道自从我发布这个问题以来已经很长时间了,我说我会在完成后发布我的解决方案. 所以在这里. 我希望有一天它会帮助某人. 这是一个完整的工作类,您只需将它放在应用程序中,并将TEMPLATE_DIRE ...

  3. api数据接口文档_接口文档示例(Taobao/jd/pinduoduo/开放接口调用)

    api数据接口文档_接口文档示例 本文主要是提供了一个接口文档的范文,内容修订历史.目录.时序图.接口要素描述.接口说明.使用示例.字典.FAQ.  使用MD格式文档(makedown),选择原因,容 ...

  4. 计算机网络校园局域网课题,毕业论文基于VLAN技术的校园局域网建设word文档_全文免费在线阅读(推荐阅读)...

    <[毕业论文]基于VLAN技术的校园局域网建设.doc>由会员分享,可免费在线阅读全文,更多与<[毕业论文]基于VLAN技术的校园局域网建设(word文档)_全文免费在线阅读> ...

  5. windchill 可交付成果 文档_敏捷等于没有文档吗?敏捷项目管理VS传统项目管理区别在哪里?...

    前阵子,一个负责产品开发的负责人跟我说,他对正在进行的产品交付项目的要求是:能够在短期内看到成果:可以随时快速地了解项目进展:不需要那么多"没必要"的设计文档,却迟迟看不到交付的功 ...

  6. swagger api文档_带有Swagger的Spring Rest API –创建文档

    swagger api文档 使REST API易于使用的真正关键是好的文档. 但是,即使您的文档做得很好,您也需要设置公司流程的权利以正确,及时地发布它. 确保利益相关者按时收到是一回事,但是您也要负 ...

  7. swagger api文档_带有Swagger的Spring Rest API –公开文档

    swagger api文档 创建API文档后,将其提供给涉众很重要. 在理想情况下,此发布的文档将足够灵活以解决任何最后的更改,并且易于分发(就成本以及完成此操作所需的时间而言). 为了使之成为可能, ...

  8. 开发之前需要哪些文档_为什么在开发之前总是应该做文档

    开发之前需要哪些文档 程序员和项目经理有时认为"文档驱动的开发"是指在开发过程中在代码中添加大量注释或与文档编写者紧密合作. 那是因为很难想象在编写文档之后开发将如何进行 ,因为肯 ...

  9. word怎么将文档分成三节_分节排版,就是将Word 2010的文档分节,使文档在不同的节中具有不同的______。(2.0分)_学小易找答案...

    [单选题]I was walking along the Qiantang River when I noticed the beautiful sun setting. I snapped a fe ...

最新文章

  1. 【生成模型】解读显式生成模型之完全可见置信网络FVBN
  2. 计算机要学打字吗,有了电脑打字,还需要练字吗?
  3. linux打补丁前如何备份,关于Linux下给文件打补丁
  4. matlab 与dsp联合仿真,matlab和DSP联合开发前景很大?
  5. 58.配置tomcat监听80 虚拟主机 日志
  6. 类的无参方法和Doc注释
  7. 分阶段付款 学php,项目整理-支付宝的支付问题
  8. 编程萌新注意:别再这样问问题了!学会这样快速定位错误内容
  9. Spring-@Value
  10. JsonCpp常见用法
  11. js function
  12. java 实现双人五子棋
  13. 年终总结--时光如水,生命如歌
  14. c# 去掉字符串最后一个逗号
  15. 人体动作捕捉-坐标转换
  16. oracle避免回表,请教大家: 经常在sql调优过程中提到避免“回表”,具体是指什么意思???3Q...
  17. 计算机视觉标定方法,计算机视觉测量中摄像机快速标定方法实现
  18. vb.net 教程 3-10 窗体编程 datagridview控件 1 初步
  19. 2022年网络工程师考试知识点:广域网技术
  20. 编译器-LL(1)-First集与Follow集

热门文章

  1. Perforce-Server迁移
  2. dos中进入其他盘中的方法
  3. HDU 4534 郑厂长系列故事——新闻净化
  4. 关于C语言中文件的基本操作
  5. [转载] Python Pandas 的 all和any方法
  6. [转载] Python一行代码实现1到100之和
  7. [转载] 使用Python中的NLTK和spaCy删除停用词与文本标准化
  8. 盐池元宵转九曲【山乡元夕】
  9. python循环语句笔记
  10. java.util.HashMap