我是否误解了assert()的用法?

发布于 2024-09-10 18:10:16 字数 1745 浏览 7 评论 0原文

我正在查看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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-09-17 18:10:16

先生,你说得完全正确。这是 assert 的错误用法。

You're perfectly right sir. This is a poor usage of assert.

青巷忧颜 2024-09-17 18:10:16

你确实是对的。正如其他人已经指出的那样,assert() 很可能会在发布版本中编译出来(我见过人们强制将断言保留在发布版本中)。

我只是想添加一个与我在代码库中看到的问题相关的恐怖故事:

assert(do_something() == NO_ERR);

不应允许某些人使用键盘。

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:

assert(do_something() == NO_ERR);

Some people should not be allowed to use a keyboard.

莫多说 2024-09-17 18:10:16

小通知:如果你写的话会更好。

FILE * datafile = NULL;

另外,断言只能在调试模式下工作......所以你的方法更好。

Minor notification: it would be better if you write..

FILE * datafile = NULL;

Also, assert only works in debug mode... so your method is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文