Using the C Math Library | ![]() ![]() |
Function Template
You can use the template code below as the basis for writing functions that use the library's automated memory management. You can find the general function template and a main
routine template in <matlab>/extern/examples/cmath/mem_mgt_func_template.c
and mem_mgt_main_template.c
respectively where <matlab>
represents the top-level directory of your installation.
This function template takes one array output argument, two array input arguments, and returns an array. Your functions will, of course, vary the number of input and output arguments.
Notice how mlfEnterNewContext()
and mlfRestorePreviousContext()
operate on the array arguments passed to FunctionName
. mlfReturnValue()
manipulates the array returned from FunctionName
.
mxArray *FunctionName(mxArray **output_arg1, mxArray *input_arg1, mxArray *input_arg2) { mxArray *local_return_value = NULL; mxArray *local_var1 = NULL; mxArray *local_var2 = NULL;mlfEnterNewContext(1,2, output_arg1, input_arg1, input_arg2);
/* Perform the work of the function. */ /* .... */ /* Note: Don't destroy local_return_value */ mxDestroyArray(local_var1); mxDestroyArray(local_var2);mlfRestorePreviousContext(1,2,output_arg1, input_arg1, input_arg2);
return mlfReturnValue(local_return_value);
}
Main Routine Template
The template for the main()
routine is different from the general template because main()
does not take any array input or output arguments or return an array. Passing 0
as an argument to mlfEnterNewContext()
and mlfRestorePreviousContext()
indicates that main()
has no output and input array arguments. A call to mlfReturnValue()
is not required.
int main() { /* Initialize variables. */mlfEnterNewContext(0,0);
/* Perform the work of main(). */mlfRestorePreviousContext(0,0);
return(EXIT_SUCCESS); }
![]() | Using Automated Memory Management | Enabling Memory Management | ![]() |