使用断言处理错误检查 [英] Handling error checking with assert

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

问题描述

我已经看了很多遍,似乎在assert上有很多不同的观点.例如,如果我正在分配一个指针,并想确保它已正确分配,我会写:

I've looked all over and it seems that there are a lot of mixed views on assert. For example, if I'm malloc'ing a pointer and want to make sure it's been allocated correctly I'd write:

p = malloc(sizeof(int));
assert(p)

代替:

p = malloc(sizeof(int));
if (p == NULL)
{
... send error message
}

我知道断言会终止程序,但出于测试目的-我想知道的是绝对安全的方法:

I know that with assert it will end the program, but for testing purposes — what I want to know is what the absolute safest way of:

  1. 测试是否正确完成了malloc之类的事情.
  2. 如果未正确输入malloc,则会发生错误.
  1. testing for things like a malloc being done correctly.
  2. dealing with an error if something isn't malloc'd correctly.

推荐答案

  1. 测试失败:
    如果无法将所需的内存量提供给程序,则C标准要求malloc函数返回NULL.

    1. TESTING FOR THE FAILURE:
      The malloc function is REQUIRED by the C standard to return NULL if the requested amount of memory cannot be given to the program.

      这意味着,如果malloc的返回值为非NULL,则可以确保正确分配了内存的 ALL .

      That means that if the return value of malloc is non-NULL, you can be sure that ALL of the memory was properly allocated.

      检查NULL返回值是确定malloc是否成功的唯一方法. assert函数可用于在断言失败的情况下停止程序,但是在程序的正式版中, 必须有其他错误处理.

      Checking for a NULL return value is the ONLY way to determine whether or not malloc was successful. The assert function can be used to stop the program if an assertion fails, but in a production release of the program, there must be other error handling.

    2. 处理故障:
      如果返回值为NULL,则使用errno变量来确定为什么会发生故障. errno变量也是C标准的一部分.

    3. HANDLING THE FAILURE:
      If the return value is NULL, use the errno variable to determine WHY the failure occurred. The errno variable is also part of the C standard.

      对于LINUX,这是值列表errno可以设置为:

      For LINUX, here is the list of values errno can be set to:

      http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html

    4. 重要:malloc失败是一个严重的错误.如果发生这种情况 程序执行时,请勿尝试继续执行其他程序 程序中的功能. 记录错误后立即停止(退出)程序,然后 向程序用户报告,如下所示:

      IMPORTANT: A malloc failure is a serious error. If this happens in the program execution, don't try to continue to execute additional functionality in the program. Stop (exit from) the program as soon as an error has been logged and reported to the user of the program, as follows:

      您应该将exit函数与非零返回值一起使用 值,以通知程序用户该程序已退出 状态错误. exit函数也是 C语言标准的一部分.

      You should use the exit function with a non-zero return value to notify the user of the program that the program exited with an error status. The exit function is ALSO part of the C language standard.

      此外,在退出程序之前,请确保所有其他内存 分配(在malloc失败之前)已正确地取消分配.

      Also, before you exit the program, make sure all other memory that was allocated (prior to the malloc failure) is properly de-allocated.

    5. 这篇关于使用断言处理错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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