我是否误解了assert()的用法?
我正在查看assert()
参考页< /a> 当我阅读给定的示例时,我陷入了困境:
/* assert example */
#include <stdio.h>
#include <assert.h>
int main ()
{
FILE * datafile;
datafile=fopen ("file.dat","r");
assert (datafile);
fclose (datafile);
return 0;
}
在此示例中,断言用于在数据文件比较等于 0 时中止程序执行,这种情况发生在先前对 fopen 的调用不成功时。
我完全同意,如果 fopen()
失败,assert()
将中止执行。但是我担心这个例子的正确性:
在我看来,assert()是用来检测通常不会发生的情况的(比如传递一个NULL
指向一个函数的指针,该函数的文档声明它是被禁止的)。
在此示例中,无法打开文件并不是通常不会发生的情况。事实上,我可以看到很多失败的原因。文件不能存在,程序可以在没有所需权限的情况下运行,等等。
我宁愿做类似的事情:
/* not longer an assert example */
#include <stdio.h>
#include <assert.h>
int main ()
{
FILE * datafile;
datafile=fopen ("file.dat","r");
if (datafile != NULL)
{
// Do something, whatever.
fclose (datafile);
} else
{
// Report the error somehow.
}
return 0;
}
我对 assert()
应该如何使用的理解是否不正确?
编辑和好消息!
看来所提到的网站是由严格的人统治的。这是我从一位网站维护人员那里收到的邮件:
嗨朱利安,我必须同意, 示例代码选择不当。它有 现在刚刚被重写为某些东西 比较合适。
非常感谢 指出这一点,并对任何 这可能给您带来的不便 你。
最诚挚的问候,
以及更新的示例:
/* 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;
}
很高兴看到有些人在互联网上做得很好! ;)
I was taking a look at the assert()
reference page and I got stuck while I read the given example:
/* assert example */
#include <stdio.h>
#include <assert.h>
int main ()
{
FILE * datafile;
datafile=fopen ("file.dat","r");
assert (datafile);
fclose (datafile);
return 0;
}
In this example, assert is used to abort the program execution if datafile compares equal to 0, which happens when the previous call to fopen was not successful.
I totally agree that if fopen()
fails, assert()
will abort execution. However I'm concerned about the rightness of this example:
In my opinion assert()
is there to detect cases that can't normally happen (like passing a NULL
pointer to a function whose documentation states it is forbidden).
In this example, failing to open a file is not something that can't normally happen. In fact, I can see dozens of reasons why this would fail. The file couldn't exist, the program could run without required privileges, and so on.
I would rather have done something like:
/* not longer an assert example */
#include <stdio.h>
#include <assert.h>
int main ()
{
FILE * datafile;
datafile=fopen ("file.dat","r");
if (datafile != NULL)
{
// Do something, whatever.
fclose (datafile);
} else
{
// Report the error somehow.
}
return 0;
}
Is my understanding of how assert()
should be used incorrect ?
EDIT AND GOOD NEWS !
It seems the referred site is ruled by rigorous people. Here is the mail I got back from one of the site maintainer:
Hi Julien, I have to agree, the
example code was poorly chosen. It has
now been just rewritten to something
more appropriate.Many thanks for
pointing this out, and sorry for any
inconveniences this may have caused to
you.Best regards,
And the updated example:
/* 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;
}
Glad to see that some people do their work well on the Internet ! ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
先生,你说得完全正确。这是
assert
的错误用法。You're perfectly right sir. This is a poor usage of
assert
.你确实是对的。正如其他人已经指出的那样,assert() 很可能会在发布版本中编译出来(我见过人们强制将断言保留在发布版本中)。
我只是想添加一个与我在代码库中看到的问题相关的恐怖故事:
不应允许某些人使用键盘。
You are right indeed. As other people have already pointed out,
assert()
will be more than likely compiled out in the release build (I've seen people force the asserts to be left in for the release build).I just wanted to add a horror story related to this question that I've seen on a code-base:
Some people should not be allowed to use a keyboard.
小通知:如果你写的话会更好。
另外,断言只能在调试模式下工作......所以你的方法更好。
Minor notification: it would be better if you write..
Also, assert only works in debug mode... so your method is better.