CH3___Debugging C++ Programs

2023-11-14

3.1 — Syntax and semantic errors

Modern compilers have been getting better at detecting certain types of common semantic errors (e.g. use of an uninitialized variable). However, in most cases, the compiler will not be able to catch most of these types of problems, because the compiler is designed to enforce grammar, not intent.

3.2 — The debugging process

A general approach to debugging

Once a problem has been identified, debugging the problem generally consists of five steps:

1.Find the root cause of the problem (usually the line of code that’s not working)
2.Ensure you understand why the issue is occurring
3.Determine how you’ll fix the issue
4.Repair the issue causing the problem
5.Retest to ensure the problem has been fixed and no new problems have emerged

3.3 — A strategy for debugging

Finding problems via code inspection


Finding problems by running the program


Reproducing the problem


Homing in on issues

3.4 — Basic debugging tactics

Debugging tactic #1: Commenting out your code


Debugging tactic #2: Validating your code flow


Debugging tactic #3: Printing values


Why using printing statements to debug isn’t great

3.5 — More debugging tactics

Conditionalizing your debugging code


Using a logger

3.6 — Using an integrated debugger: Stepping

When you run your program, execution begins at the top of the main function, and then proceeds sequentially statement by statement, until the program ends. At any point in time while your program is running, the program is keeping track of a lot of things: the value of the variables you’re using, which functions have been called (so that when those functions return, the program will know where to go back to), and the current point of execution within the program (so it knows which statement to execute next). All of this tracked information is called your program state (or just state, for short).

The debugger


Stepping


Step into


Step over


Step out


A step too far


Step back

3.7 — Using an integrated debugger: Running and breakpoints

Run to cursor

Continue

Start

Breakpoints

Set next statement

A breakpoint is a special marker that tells the debugger to stop execution of the program at the breakpoint when running in debug mode.

3.8 — Using an integrated debugger: Watching variables

3.9 — Using an integrated debugger: The call stack

The call stack is useful in conjunction with breakpoints, when your breakpoint is hit and you want to know what functions were called to get to that specific point in the code.

3.10 — Finding issues before they become problems

Don’t make errors

Well, the best thing is to not make errors in the first place. Here’s an incomplete list of things that can help avoid making errors:

Follow best practices
Don’t program when tired
Understand where the common pitfalls are in a language (all those things we warn you not to do)
Keep your programs simple
Don’t let your functions get too long
Prefer using the standard library to writing your own code, when possible
Comment your code liberally


Refactoring your code

Key insight

When making changes to your code, make behavioral changes OR structural changes, and then retest for correctness. Making behavioral and structural changes at the same time tends to lead to more errors as well as errors that are harder to find.

An introduction to defensive programming

Defensive programming is a practice whereby the programmer tries to anticipate all of the ways the software could be misused, either by end-users, or by other developers (including the programmer themselves) using the code. These misuses can often be detected and then mitigated (e.g. by asking a user who entered bad input to try again).


Finding errors fast


An introduction to testing functions


An introduction to constraints


Shotgunning for general issues

3.x — Chapter 3 summary and quiz

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CH3___Debugging C++ Programs 的相关文章

  • 具有子列表属性映射问题的自动映射器

    我有以下型号 Models public class Dish Required public Int64 ID get set Required public string Name get set Required public str
  • OpenCv读/写视频色差

    我试图简单地使用 openCV 打开视频 处理帧并将处理后的帧写入新的视频文件 我的问题是 即使我根本不处理帧 只是打开视频 使用 VideoCapture 读取帧并使用 VideoWriter 将它们写入新文件 输出文件看起来比输入更 绿
  • asp.net 文本框文本模式数字,仅允许数字

    我只是想知道 ASP NET 中是否有一种方法只允许文本框中的数字textmode number 当我使用这个时
  • 迭代变量并查找特定类型实例的技术

    我想迭代进程中内存中的变量 通过插件动态加载 并查找特定类型的实例 以前我可以找到特定类型 或内存中的所有类型 我可以创建类型的实例 我可以获取作为不同类型的字段包含的实例 但我无论如何都不知道只是 搜索 特定类型的实例 一种方法是使用 W
  • 使用具有现有访问令牌的 Google API .NET 客户端

    用例如下 移动应用程序正在通过 Google 对用户进行身份验证 并且在某些时候 我们需要将用户的视频发布到他的 YouTube 帐户 出于实际原因 实际发布应该由后端完成 已经存储在那里的大文件 由于用户已经通过应用程序的身份验证 因此应
  • 使用post方法将多个参数发送到asp.net core 3 mvc操作

    使用 http post 方法向 asp net mvc core 3 操作发送具有多个参数的 ajax 请求时存在问题 参数不绑定 在 dot net 框架 asp net web api 中存在类似的限制 但在 asp net mvc
  • 从多个类访问串行端口

    我正在尝试使用串行端口在 arduino 和 C 程序之间进行通信 我对 C 编程有点陌生 该程序有多种用户控制形式 每一个都需要访问串口来发送数据 我需要做的就是从每个类的主窗体中写入串行端口 我了解如何设置和写入串行端口 这是我的 Fo
  • C 语言中 =+(等于加)是什么意思?

    我碰到 与标准相反 今天在一些 C 代码中 我不太确定这里发生了什么 我在文档中也找不到它 In ancientC 版本 相当于 它的残余物与最早的恐龙骨头一起被发现 例如 B 引入了广义赋值运算符 使用x y to add y to x
  • 即使手动设置显示环境变量后,WSL Ubuntu 也会显示“错误:无法打开显示”

    我在 WSL Ubuntu 上使用 g 我使用 git 克隆了 GLFW 存储库 使用了ccmake命令配置并生成二进制文件 然后使用make在 build 目录中最终创建 a文件 我安装了所有OpenGL相关的库 usr ld 我不记得我
  • Qt 创建布局并动态添加小部件到布局

    我正在尝试在 MainWindow 类中动态创建布局 我有四个框架 它们是用网格布局对象放置的 每个框架都包含一个自定义的 ClockWidget 我希望 ClockWidget 对象在调整主窗口大小时相应地调整大小 因此我需要将它们添加到
  • 将数据打印到文件

    我已经超载了 lt lt 运算符 使其写入文件并写入控制台 我已经为同一个函数创建了 8 个线程 并且我想输出 hello hi 如果我在无限循环中运行这个线程例程 文件中的o p是 hello hi hello hi hello hi e
  • C# 中条件编译符号的编译时检查(参见示例)?

    在 C C 中你可以这样做 define IN USE 1 define NOT IN USE 1 define USING system 1 system 1 IN USE 进而 define MY SYSTEM IN USE if US
  • 将构建日期放入“关于”框中

    我有一个带有 关于 框的 C WinForms 应用程序 我使用以下方法将版本号放入 关于 框中 FileVersionInfo GetVersionInfo Assembly GetExecutingAssembly Location F
  • 当“int”处于最大值并使用 postfix ++ 进行测试时,代码定义良好吗?

    示例 未定义行为的一个示例是整数溢出的行为 C11dr 3 4 3 3 int溢出是未定义的行为 但这是否适用于存在循环的以下内容 并且不使用现在超出范围的副作用i 特别是 这是否后缀增量规格帮助 结果的值计算在副作用之前排序 更新操作数的
  • 当我“绘制”线条时,如何将点平均分配到 LineRenderer 的宽度曲线?

    我正在使用线条渲染器创建一个 绘图 应用程序 现在我尝试使用线条渲染器上的宽度曲线启用笔压 问题在于 AnimationCurve 的 时间 值 水平轴 从 0 标准化为 1 因此我不能在每次添加位置时都在其末尾添加一个值 除非有一个我不知
  • 获取 2 个数据集 c# 中的差异

    我正在编写一个简短的算法 它必须比较两个数据集 以便可以进一步处理两者之间的差异 我尝试通过合并这两个数据集并将结果更改放入新的数据集来实现此目标 我的方法如下所示 private DataSet ComputateDiff DataSet
  • 尚未处理时调用 Form 的 Invoke 时出现 ObjectDisposeException

    我们得到一个ObjectDisposedException从一个电话到Invoke在尚未处理的表格上 这是一些演示该问题的示例代码 public partial class Form2 Form void Form2 Load object
  • 当前的 x86 架构是否支持非临时加载(来自“正常”内存)?

    我知道有关此主题的多个问题 但是 我没有看到任何明确的答案或任何基准测量 因此 我创建了一个处理两个整数数组的简单程序 第一个数组a非常大 64 MB 第二个数组b很小 无法放入 L1 缓存 程序迭代a并将其元素添加到相应的元素中b在模块化
  • 使用 C# 从 DateTime 获取日期

    愚蠢的问题 给定日期时间中的日期 我知道它是星期二 例如我如何知道它的 tue 2 和 mon 1 等 Thanks 您正在寻找星期几 http msdn microsoft com en us library system datetim
  • 实例化 Microsoft.Office.Interop.Excel.Application 对象时出现错误:800700c1

    实例化 Microsoft Office Interop Excel Application 以从 winforms 应用程序生成 Excel 时 出现以下错误 这之前是有效的 但突然间它停止工作了 尽管代码和 Excel 版本没有变化 我

随机推荐

  • [云原生专题-22]:K8S - 集群编排工具K8S与SWARM比较与技术选择

    作者主页 文火冰糖的硅基工坊 文火冰糖 王文兵 的博客 文火冰糖的硅基工坊 CSDN博客 本文网址 https blog csdn net HiWangWenBing article details 122750196 目录 前言 第1章
  • Tomcat安装及IDEA配置Tomcat教程

    Tomcat安装 以Tomcat8 5为例 1 网站链接 Apache Tomcat Apache Tomcat 8 Software Downloads 根据个人喜好 我安装的是8 5版本 2 下载完解压即可 我的安装目录为 E Envi
  • R语言入门(一)

    R语言入门 一 在vscode中使用R语言注意事项 按照其他教程安装配置插件 使用run source运行r代码 使用run code 会报错 原因未知 生成正态分布的点数据 import pandas as pd import numpy
  • win7+ VS2010安装CUDA7.0图文说明

    win7 VS2010安装CUDA7 0图文说明 1 查看本机配置 查看显卡类型是否支持NVIDIA GPU 选中计算机 gt 右键属性 gt 设备管理器 gt 显示适配器 NVIDIA GeForce GT 610 从https deve
  • Servlet 基础知识(4)(利用Servlet实现文件上传功能)

    目录 1 DiskFileUpload 类 1 setSizeMax 方法 2 setSizeThreshold 方法 3 setRepositoryPath 方法 4 parseRequest HttpServletRequest req
  • TOOLS_Python获取音域范围

    基于librosa pyin方法 链接 获取基频最值 对比标准音高序列 得到音域范围 def create standard pitch sequence 生成一个包含名称的标准音高序列 T C C D D D E E F F G G G
  • 第七届蓝桥杯省赛C++B组 最大比例

    最大比例 X星球的某个大奖赛设了M级奖励 每个级别的奖金是一个正整数 并且 相邻的两个级别间的比例是个固定值 也就是说 所有级别的奖金数构成了一个等比数列 比如 16 24 36 54 其等比值为 3 2 现在 我们随机调查了一些获奖者的奖
  • sublime text3 安装 golangsublime 配置

    1 安装git 因为golang是通过git来管理远程包的 所以我们首先要安装git 下载地址 http www git scm com download git安装比较简单 直接下一步即可 在Windows Explorer integr
  • Spring Boot:如何配置Undertow容器?不会我教你

    环境说明 Windows10 Idea2021 3 2 Jdk1 8 SpringBoot 2 3 1 RELEASE 一 前言 作为springboot开发者 使用最多的就是Tomcat 这是springboot默认的容器技术 而且是内嵌
  • MyEclipse修改.properties文件的编码

    MyEclipse中新建一个messageResource properties文件 如果输入中文保存时就会提示错误 Save could not be completed Reason some characters cannot be
  • 京东零售大佬为你讲解:黑盒测试的底层逻辑

    什么是黑盒测试 它是把程序看作一个黑盒子 在不考虑程序内部结构的情况下 检查程序功能是否按照PRD的规定正常使用 程序是否能适当地接收输入数据 产生正确的输出 这其实就是黑盒测试的定义 也是黑盒测试的底层逻辑 一般人不会重视定义 但往往就是
  • html5 canvas(小树姐的牛掰到爆了的作品)

    自从小树嫁了个牛逼的前端之后 canvas的境界超过我了 小树demo 小编表示 这个境界 这个几何 让我有种跪舔的感觉 http www wow trend com brand index shtml 这个hover让我彻底凌乱了 div
  • react中Hooks

    React Hook Hooks是什么 常见的Hook 1 state Hook 2 Effect Hook 3 Ref Hook 4 Context Hook React Hook Hooks是什么 1 Hook是react 16 8版本
  • Qt的ui文件不能简单复制

    在使用vs Qt开发时 直接复制另外一个widget类的ui文件 简单改名成当前类对应的ui文件 会导致编译出错 尽可能使用添加的Qt class自带的ui文件 因为ui文件的配置文件中有许多与当前类相关的字符串 简单复制容易报错
  • 二叉树的结点数

    二叉树的结点数 10分 已知二叉树的结点结构定义如下 typedef struct NODE char data struct NODE lch rch NODE 说明 data 为数据域 均为英文大写字母 lch 和 rch 分别为指示左
  • 抖音视频怎么去水印

    水印 一般是指放置在图片 视频或者文档上的文字或者图标 用来做标记或者品牌宣传 我们从网上获取的文件资源很多都是带有水印的 比如从抖音短视频下载的视频就会带有水印 为了达到更好的观看效果 我们就需要将这些视频自带的水印给去除掉 下面就来教教
  • Unity Shader渲染顺序 坐标系 和光照模型

    1 Shader中的渲染顺序 是按照Queue Geometry RenderType Opaque Queue是一般渲染时候的顺序 RenderType是后处理特效使用的渲染顺序 Background Geometry AlphaTest
  • JAVA中XML格式字符串转为javabean(对象),然后返回xml格式字符串

    一 引入相关依赖 pom xml文件配置如下所示
  • Road Construction POJ - 3352(tarjan双连通缩点模板)

    题目描述 给一个无向连通图 至少添加几条边使得去掉图中任意一条边不改变图的连通性 即使得它变为边双连通图 include
  • CH3___Debugging C++ Programs

    3 1 Syntax and semantic errors Modern compilers have been getting better at detecting certain types of common semantic e