Came across the following struct which is defined in tcl.h, the C language API for Tcl.
typedef struct {
char *result;
Tcl_FreeProc *freeProc;
int errorLine;
} Tcl_Interp;
typedef void Tcl_FreeProc(char *blockPtr);
In the documentation, they have mentioned the following on the struct member fields:
The result field points to the string that represents the result or error message, and the freeProc field tells how to dispose of the storage for the string when it is not needed anymore.
Tcl_SetResult’s freeProc argument specifies how the Tcl system is to manage the storage for the result argument. If Tcl_SetResult or Tcl_SetObjResult are called at a time when interp holds a string result, they do whatever is necessary to dispose of the old string result.
freeProc can take three main values, TCL_STATIC, TCL_DYNAMIC or TCL_VOLATILE.
If freeProc is not one of the values TCL_STATIC, TCL_DYNAMIC, and TCL_VOLATILE, then it is the address of a procedure that Tcl should call to free the string. This allows applications to use non-standard storage allocators.
The fields are defined in the tcl.h header:
#define TCL_VOLATILE ((Tcl_FreeProc *) -1)
#define TCL_STATIC ((Tcl_FreeProc *) 0)
#define TCL_DYNAMIC ((Tcl_FreeProc *) free)
What I can’t understand is how they have defined the three values. Shouldn’t it be a function pointer with void return and one char* argument? how can it take values like 0 and -1?
The Tcl_FreeProc is a pointer, as a pointer it can be assigned any value.
In this case the pointer gets assigned either -1, 0 or a real address and since -1 and 0 are not valid addresses is used to check whether the freeProc points to a function (which needs to be called) or has one of those values and as such no free function is needed.
EDIT: rephrased it for Chris.