Extremely pedantic, but however...

F

Frederick Gotham

ozbear posted:
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)
 
D

Dik T. Winter

> void *p_SAFE_FREE; ....
> 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 &&.

I think the complaint is the other way around. What is the type of the
argument of malloc?
 
F

Frederick Gotham

Dik T. Winter posted:
I think the complaint is the other way around. What is the type of the
argument of malloc?


Wups! Obviously I should have written something like:

SAFE_FREE( malloc(512) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top