How to handle realloc when it fails due to memory?

后端 未结 8 900
不思量自难忘°
不思量自难忘° 2020-11-27 19:18

Question says it all but here is an example:

typedef struct mutable_t{
    int count, max;
    void **data;
} mutable_t;


void pushMutable(mutable_t *m, voi         


        
8条回答
  •  长情又很酷
    2020-11-27 19:33

    This is a bit of a hot button topic as there are essentially 2 schools of thought on the subject

    1. Detect the OOM, and having the function return an error code.
    2. Detect the OOM and crash your process as fast as possible

    Personally I am in camp #2. Expect for very special types of applications, OOM is fatal period. True, perfectly written code can handle an OOM but so few people understand how to write code that is safe in the face of no memory. Even fewer bother to actually do it because it's almost never worth the effort.

    I dislike passing the error code off to the calling function for OOM's because it is the equivalent of telling the caller "I failed and there's nothing you can do about it". Instead I prefer to crash fast so the resulting dump is as instructive as possible.

提交回复
热议问题