Using the C Math Library | ![]() ![]() |
Example Without Automated Memory Management
The function Explicit_Mem_Example()
performs the same calculations as Automated_Mem_Example()
in the previous example. Compare the use of temporary variables, nonnested calls to the MATLAB C Math Library functions, and calls to mxDestroyArray()
. It contains twenty-six lines of code compared to Automated_Mem_Example()
's nine lines.
mxArray *Explicit_Mem_Example(mxArray **z_out, mxArray *x_in, mxArray *y_in) { mxArray *result_local, *q_local; mxArray *temp1, *temp2, *temp3; /* In MATLAB: r = sqrt(sin(x) + cos(x)) */ temp1 = mlfSin(x_in); temp2 = mlfCos(y_in); temp3 = mlfPlus(temp1, temp2); result_local = mlfSqrt(temp3); mxDestroyArray(temp1); mxDestroyArray(temp2); mxDestroyArray(temp3); /* In MATLAB: q = sqrt(cos(y) - sin(y)) */ temp1 = mlfCos(y_in); temp2 = mlfSin(y_in); temp3 = mlfMinus(temp1, temp2); q_local = mlfSqrt(temp3); mxDestroyArray(temp1); mxDestroyArray(temp2); mxDestroyArray(temp3); /* In MATLAB: z = q * r - q^3 */ temp1 = mlfScalar(3); temp2 = mlfPower(q_local, temp1); temp3 = mlfTimes(q_local, result_local); *z_out = mlfMinus(temp3, temp2); mxDestroyArray(temp1); mxDestroyArray(temp2); mxDestroyArray(temp3); mxDestroyArray(q_local); return result_local; }
![]() | Example - Managing Array Memory (ex2.c) | Restrictions | ![]() |