Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2006:
[Freeciv-Dev] Re: (PR#15126) patch: fc_free macro
Home

[Freeciv-Dev] Re: (PR#15126) patch: fc_free macro

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: cp.ml.freeciv.dev@xxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#15126) patch: fc_free macro
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 10 Jan 2006 12:10:08 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=15126 >

Christian Prochaska wrote:
> <URL: http://bugs.freeciv.org/Ticket/Display.html?id=15126 >
> 
>>[jdorje - Di 10. Jan 2006, 19:00:23]:
>>
>>Such a macro violates the rules of function-looking macros.  That is, it 
>>looks like a function but it behaves as no function ever could.
>>
>>If we have it (code like this is used in several places) it should be 
>>FC_FREE().
>>
>>-jason
>>
> 
> 
> The macro in gui-sdl/gui_mem.h is actually
> 
> #define FREE(ptr) do { if (ptr) free(ptr); ptr = NULL; } while(0)
> 
> So as I now understand the do {} while(0) construct is just to make it
> not look like a function and thus the macro in utility/mem.h should be
> 
> #define FC_FREE(ptr) do { free(ptr); ptr = NULL; } while(0)
> 
> correct?

The do...while construct is to make it act like a single statement.  For 
instance in

   if (x)
     FREE(y);
   else
     FREE(z);

without the do...while bits the code is quite a bit different, even if 
you use brackets in the FREE (as your original did).

-jason





[Prev in Thread] Current Thread [Next in Thread]