3 ms·
From my understanding, his problem was that the malloc wrapper being used not only checked but also returned -1 if it was null (I assume some other arbitrary n
by ianbishop 17y ago
From my understanding, his problem was that the malloc wrapper being used not only checked but also returned -1 if it was null (I assume some other arbitrary number otherwise) which would add extra computation time.
Out of curiosity, I have always used assert to do malloc null checks. I realize it is just a macro which gives a conditional to exit but is at all hindering in terms of performance?
eg:
void *Malloc(size_t size) {
void *r = malloc(size);
assert(r != null);
return r;
}
- dbenamy 17y agoThat's probably the same as the non-assert check EXCEPT that release builds might disable assertions and then you're in trouble.
- basugasubaku 17y agoRight. Plus as a matter of style, assertions are traditionally distinguished from error handling. Assertions are used to catch programmer errors; error handling is for when something happens that is nevertheless within the contract of the library (malloc can return NULL, fopen can return NULL, etc).
- ori_b 17y agothe problem is that assert is defined so that if NDEBUG is defined, it's a no-op. I'd use an explicit check and an abort() call.