我误解断言()的用法? [英] Am I misunderstanding assert() usage?

查看:132
本文介绍了我误解断言()的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看断言()引用页面而我读给定的例子我卡住了:

  / *断言例如* /
#包括LT&;&stdio.h中GT;
#包括LT&;&ASSERT.H GT;诠释的main()
{
  FILE *数据文件;
  数据文件= FOPEN(FILE.DAT,R);
  断言(数据文件);  FCLOSE(数据文件);  返回0;
}


  

在这个例子中,断言用于如果数据文件相比较等于0,这发生在当previous呼叫给fopen不成功,中止程序执行


我完全同意,如果 fopen()函数失败,断言()将中止执行。不过,我很担心这个例子的正确性:

在我看来断言()有没有检测到的情况下通常不能(如传递 NULL 指向的文件指出它是被禁止的函数)。

在这个例子中,没有打开一个文件是不是的东西通常不能。其实,我可以看到几十个原因,这将会失败。该文件可能不存在,程序可以而无需权限运行,等等。

我宁愿这样做是这样的:

  / *不再断言例如* /
#包括LT&;&stdio.h中GT;
#包括LT&;&ASSERT.H GT;诠释的main()
{
  FILE *数据文件;
  数据文件= FOPEN(FILE.DAT,R);  如果(数据文件!= NULL)
  {
    //做一些事情,不管。
    FCLOSE(数据文件);
  }其他
  {
    //不知何故报告错误。
  }  返回0;
}

是我的理解是如何断言()应使用不正确的?


修改的喜讯!

看来提到的网站是通过严格的人统治。下面是我从网站维护者之一回来邮件:


  

朱利安我不得不承认,在
  例如code的选择很差。它有
  现在已经只是重写的东西
  比较合适的。


  
  

有关非常感谢
  指出这一点,而对于任何遗憾
  不便这可能导致
  你。


  
  

最好的问候,


和更新的例子:

  / *断言例如* /
#包括LT&;&stdio.h中GT;
#包括LT&;&ASSERT.H GT;无效print_number为(int *敏){
  断言(敏!= NULL);
  的printf(%d个\\ N,*敏);
}诠释的main()
{
  INT A = 10;
  为int * B = NULL;
  为int * c = NULL;  B =&放大器;一个;  print_number(二);
  print_number(C);  返回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 ! ;)

解决方案

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

这篇关于我误解断言()的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆