What is the correct way to handle “out of memory”?

2020-02-26 01:32发布

Recently, I work on a video player program on Windows for a CCTV program. As the program has to decode and play many videos streams at the same time, I think it might meet the situation that malloc will fail and I add checking after every malloc.

But genrally speaking, in these code of open source programs that I've read in open source projects, I seldom find any checking of result of malloc. So when malloc fails, most program will just crash. Isn't that unacceptalbe?

My colleagues who write server programs on linux will alloc a enough memory for 100 client connections. So although his program might refuse the 101 client, it will never met a failure of malloc. Is his approach also suitable for desktop applications?

标签: c malloc
7条回答
欢心
2楼-- · 2020-02-26 02:13

Always check, and pre-allocate a buffer that can be freed in this case so you can warn the user to save his data and shut down the application.

查看更多
登录 后发表回答