F
Frederick Gotham
ozbear posted:
I realise that. The macro name is written in ALL CAPS though, so the
programmer is made aware of the dangers.
Alternative forms would be:
inline void safe_free(void *const p)
{
if (p) free(p);
}
Or, the non-thread-safe:
void *p_SAFE_FREE;
#define SAFE_FREE(p) (\
\
(p_SAFE_FREE = (p)), \
\
((void)(p_SAFE_FREE && free(p_SAFE_FREE))) )
int main()
{
SAFE_FREE( malloc(p_SAFE_FREE) );
}
(I don't know how my compiler is parsing that, but it's complaing about a
conversion from size_t to void* -- I'd say its something to do with the &&.
Or, the non-expression one:
#define SAFE_FREE(p) do{void*const q=(p);q && free(q);}while(0)
Invoking your FREE_SAFE macro above with an argument of malloc(10000)
generates a nice memory leak.
I realise that. The macro name is written in ALL CAPS though, so the
programmer is made aware of the dangers.
Alternative forms would be:
inline void safe_free(void *const p)
{
if (p) free(p);
}
Or, the non-thread-safe:
void *p_SAFE_FREE;
#define SAFE_FREE(p) (\
\
(p_SAFE_FREE = (p)), \
\
((void)(p_SAFE_FREE && free(p_SAFE_FREE))) )
int main()
{
SAFE_FREE( malloc(p_SAFE_FREE) );
}
(I don't know how my compiler is parsing that, but it's complaing about a
conversion from size_t to void* -- I'd say its something to do with the &&.
Or, the non-expression one:
#define SAFE_FREE(p) do{void*const q=(p);q && free(q);}while(0)