3 ms·
There is no generic pointer-to-pointer in C. You get an incompatible-pointer-types warning in gcc when trying to use void * * : https://godbolt.org/g/vxWFvl htt
by to3m 9y ago
There is no generic pointer-to-pointer in C. You get an incompatible-pointer-types warning in gcc when trying to use void * * : https://godbolt.org/g/vxWFvl https://godbolt.org/g/vxWFvl
There is probably scope for use of C11 _Generic, but I don't think you could support user-defined types in that way.
The usual macro problems apply when trying to do it as a macro:
#define FREE(X) (free(X),(X)=NULL,(void)0)
In C++ you can do something like this, which works well enough: (I don't use this specifically myself, but I have something similar for COM objects)
template<class T>
static void Free(T *&p) {
free(p);
p=nullptr;
}
This isn't completely useless, but it's not worth sweating blood over. Personally I think I've had more mileage from good-quality debug functionality in the memory allocator: fill newly-allocated and newly-freed regions with junk; check junk and metadata periodically; unmap regions promptly; and so on.