There are two headers in this gist which both define the destroy()
macro with the same
behavior but in slightly different ways.
##destroy.h##
This version of the destroy()
macro works with any compiler that supports __typeof__
, which
includes gcc and clang but unfortunately not MSVC. This version is completely type safe,
because no casts are used and therefore the compiler can check that all types are convertible.
##destroy_msvc.h##
This version of the destroy()
macro works on all major compilers including gcc, clang, and MSVC.
It is typesafe despite using casts because of the ErrorIfNotAtLeastDoublePointer()
macro. This is
a hacky workaround for the lack of __typeof__
in MSVC but the result is the same as the previous
version. This version is therefore more portable but is not as clean of a solution as the first.
Both versions are used in the same way and have the same result.
/* Create an array of 20 ints */
int* myArr = malloc(20 * sizeof(*myArr));
...
/* Free myArr and set it to NULL */
destroy(&myArr);