[GRASS5] Strange free statement in NVIZ

Michael Tiemann tiemann at redhat.com
Wed Jan 4 10:42:30 EST 2006


On Wed, 2006-01-04 at 15:37 +0100, Markus Neteler wrote:
> Hi,
> 
> by chance we found this strange thing:
> 
> cd visualization/nviz/src
> grep free exag.c
> extern void free();
>         free(surf_list);
>     interp->freeProc = (Tcl_FreeProc *) free;
> 
> What does the last line do?

It is like object oriented programming, but in C.  In C++, all objects
have destructors--they are implicit and do nothing if not declared--and
it is quite common that destructors are where memory that was allocated
during object initialization is deallocated (often using calls to free).
Also in C++, we have the notion of virtual destructors, which means that
the code to run at anti-initialization is determined by the type of the
object, and since type is not always apparent, this function is stored
in a function pointer that is dereferenced at run time.

The interpreter instance, interp, has a function pointer slot reserved
for freeing memory.  In this line of code, that function pointer is set
to the free function.  When called at the appropriate time, it will be
dereferenced and the effect will be calling the free function, even
though it could be any other function (depending on how the interp
instance was initialized).

M




More information about the grass-dev mailing list