如何处理免费的(c)中的错误? [英] How to handle free() errors in C?

查看:127
本文介绍了如何处理免费的(c)中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用了一个免费()函数来释放,原因是多方面的,我不能到存储。
如何从崩溃阻止我的C程序,只是生成一个错误,并继续执行?我没有的try-catch样的规定在这里(如C ++ / Java的...)。有没有办法忽略此错误并继续执行?

Suppose that I have used a free() function to free a memory that,for many reasons, I'm not allowed to. How can I stop my C application from crashing and just generate an error and continue the execution? I don't have try-catch kind of provision here (like C++/java...). Is there any way to ignore this error and continue execution?

如果是,


  1. 你是怎么做到的?

  2. 更重要的是,它是最好这样做(继续执行考虑这发生内存错误)?

感谢您

推荐答案

有什么也没有,你可以使用你想要的C标准。在免费功能的描述是说的很清楚(在C99§7.20.3.2):

There is nothing in the C standard that you can use to do what you want. The description of the free function is very clear on that (§7.20.3.2 in C99):

自由功能使空间ptr指向要被释放,也就是说,国产
  可用于进一步分配。若ptr为空指针,​​不会发生任何操作。的否则,如果
  参数不匹配的释放calloc,malloc的早期返回一个指针,或
  realloc函数,或者如果空间已释放通过调用免费或realloc,
  的行为是理解过程网络定义。

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

这篇关于如何处理免费的(c)中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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