私は C コードの多くの行をクリーンアップ ラベル/条件付きメモリ割り当ての失敗に費やしました (alloc
ファミリーが を返すことで示されますNULL
)。これは、メモリ障害が発生した場合に適切なエラー ステータスにフラグを立て、呼び出し元が「適切なメモリ クリーンアップ」を実行して再試行できるようにするための優れた方法であると教えられました。私は今、この哲学についていくつかの疑問を抱いており、それを解決したいと思っています.
I guess it's possible that a caller could deallocate excessive buffer space or strip relational objects of their data, but I find the caller rarely has the capability (or is at the appropriate level of abstraction) to do so. Also, early-returning from the called function without side effects is often non-trivial.
I also just discovered the Linux OOM killer, which seems to make these efforts totally pointless on my primary development platform.
By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer.
I figure there are probably other platforms out there that follow the same principle. Is there something pragmatic that makes checking for OOM conditions worthwhile?