什么是C编程中的内存不足处理策略? [英] What are out-of-memory handling strategies in C programming?

查看:110
本文介绍了什么是C编程中的内存不足处理策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人的一种策略是在程序启动时分配5 MB的内存(或您认为需要的任何数量).

One strategy that I though of myself is allocating 5 megabytes of memory (or whatever number you feel necessary) at the program startup.

然后,当程序的malloc()在任何时候返回NULL时,您释放5兆字节并再次调用malloc(),这将成功并让程序继续运行.

Then when at any point program's malloc() returns NULL, you free the 5 megabytes and call malloc() again, which will succeed and let the program continue running.

您对此策略有何看法?

您还知道其他哪些策略?

And what other strategies do you know?

感谢Boda Cydo.

Thanks, Boda Cydo.

推荐答案

通过正常退出来处理malloc失败.对于现代操作系统,页面文件等,您永远不应抢先做好应对内存故障的准备,而只能优雅地退出.除非您遇到算法问题,否则您不太可能会遇到内存不足的错误.

Handle malloc failures by exiting gracefully. With modern operating systems, pagefiles, etc you should never pre-emptively brace for memory failure, just exit gracefully. It is unlikely you will ever encounter out of memory errors unless you have an algorithmic problem.

此外,在启动时无缘无故地分配5MB也是疯狂的.

Also, allocating 5MB for no reason at startup is insane.

这篇关于什么是C编程中的内存不足处理策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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