Re:A critique of "How to C in 2016"
by xcgvgh on Friday January 15, 2016 @06:59AM
(#10908708)
Using calloc instead of malloc, just for checking arithmetic overflow, is superfluous. You can always write a check or a wrapper for malloc that does that automatically. It is so easy I can write it here:
(If proper warnings are enabled, it also provides extra integer type checking.)
[ Parent
| on HN ]
Agreed, but there's little point in reinventing the wheel in this particular case.
There is no reinventing going on here. In C you have to write relatively low level code. This includes manually calling allocs for objects, which includes the code for checking your arithmetic. At some point you will have to write that code. If you are smart you will wrap it into a function.
I don't see the point of the discussion, that piece of code (or relatively equivalent), is already being performed by a libC function (calloc, in this case), how is that less convenient than you writing it yourself?
It has nothing to do with how smart you are or how low level C is, you are effectively reinventing that part of calloc by wrapping malloc.
There's a point to be made that people will be idiots and risk bugs for the sake of efficiency.
That's why OpenBSD has reallocarray(3) (which serves as a fine malloc, though the kernel not having reallocation has mallocarray(9))