c++ - 为什么只检查某些值的错误? (C++?)

标签 c++ windows error-handling directx

我最近开始学习 DirectX/Windows,我正在学习的书上有代码

d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(d3d == NULL)
    //catch error &c.

我的问题是:什么会导致这一行中的错误,这与导致另一行错误的原因不同(例如,int num = 42)?

最佳答案

d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == NULL)

根据你赋予Direct3DCreate9的返回值的含义,即取决于函数的规范,这是错误与否。我写过许多指针返回函数,其中 NULL 作为返回值不是错误情况。

因此,不要将“返回 NULL 的函数”等同于“错误”。明确的错误是您的代码中的崩溃(从技术上讲,未定义的行为),例如 d3d 确实为 NULL 并且稍后您取消引用它。

int num = 42;

这里你声明了一个名为 num 的 int 变量,并用值 42 初始化它。你能想到什么样的错误?显然,num 永远不会“为 NULL”,如果这让您感到困扰的话。 42 可能是正确值,也可能是错误值,具体取决于上下文。

关于c++ - 为什么只检查某些值的错误? (C++?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1502791/

相关文章:

windows - 报告 Direct3D 内存使用情况

vb6 - VB6 中的集中错误处理

c++ - 读取 DICOM 文件时在 Release模式下出现 ITK 访问冲突错误,但在 Debug模式下不会出现 ITK 访问冲突错误

mysql - go-mysql : authentication plugin not supported while connecting from go app container to mysql container

c++ - Windows:如何获得相机支持的分辨率?

.net - 隐藏错误报告窗口

r - R : loading a table with number as the column heading all were wrongly added a X, how to solve it?

C++ 原子 : how to allow only a single thread to access a function?

c++ - 在 C++ 中初始化 boolean vector 的标准方法是什么

c++ - GLUT 键盘事件