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

后端 未结 7 976
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 13:57

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

7条回答
  •  误落风尘
    2020-12-30 14:42

    It depends on the type of application that you are working on. If the application does work that is divided into discrete tasks where an individual task can be allowed to fail, then checking memory allocations can be recovered from gracefully.

    But in many cases, the only reasonable way to respond to a malloc failure is by terminating the program. Allowing your code to just crash on the inevitable null dereference will achieve that. It would certainly always be better to dump a log entry or error message explaining the error, but in the real world we work on limited schedules. Sometimes the return on investment of pedantic error handling isn't there.

提交回复
热议问题