2 ms·
This is off topic, but your stretchy buffer does one of my pet peeves: (a)->at = realloc((a)->at, (a)->_cap * sizeof *(a)->at)) When realloc fails, the ol
by asveikau 4y ago
This is off topic, but your stretchy buffer does one of my pet peeves:
(a)->at = realloc((a)->at, (a)->_cap * sizeof *(a)->at))
When realloc fails, the old value of (a)->at will be leaked.
And memory allocation failure also leads to null pointer dereference in this code.
- deleted 4y ago[deleted]
- camel-cdr 4y agoAh, my way of dealing with memory allocation failure is to write a xmalloc/xcalloc/xrealloc that exit on error. So the idea would be that the library users can just: #unded malloc #define malloc xmalloc ... I suppose I could make this the default behavior, and make it overwritable.
- mtlmtlmtlmtl 4y agoDoesn't this break things like Valgrind?
- asveikau 4y agoFirstly, an xmalloc will call malloc and add error checks. So malloc is still called. But more fundamentally, Valgrind hooks into the allocator (and the program at large) much more deeply. You could write your own allocator (many people deploy something other than libc's) and it would still figure it out.