eekee wrote:
(I hate C++ templates too.)
I feel I have to step in here and defend C++ templates a bit.
In the context of allocation/free, while it makes no sense to template the size of an array being allocated, it very much does make sense to template the type of the array being allocated.
In my C based code, I have a FIFO, for example, which can be sized at construction. Now, to keep the code duplication at a minimum, I only support a single word size (the native word size) for this FIFO, and while FIFOs can have variable size, they're all the same type. My keyboard driver puts scan codes into such a FIFO, which is wasteful because I end up putting byte sized scan codes into a FIFO of (currently) 32-bit words. But I don't have many scan codes backed up (the FIFO is small), there is only one of them, and don't waste that much memory.
But had I used C++ templates, I could have a single body of code to support both byte and word FIFOs, with no code duplication. OK, the compiler will expand that to duplicated object code, but it's a price I'm happy to pay for the reduced maintenance and bug burden of not having duplicate source code. I'm only likely to have byte and word based FIFOs, so it's not a big code burden.
And for the record, I would use free(ptr) rather than free(ptr, length) if I indeed used free(). But I don't, 'cos I use GC for the general heap, and a bump pointer based allocator for temporary allocations that are released en-masse.