MATLAB: Output of a mex file is not the same as the recursively-written computational routine

cmexrecursive

Hi, I've recursively written a C source .mex file (function) that encodes a given input of 1xN array and outputs a 1xN encoded bit array. The encoding operation works perfectly and as desired. The problem, however, is at the gateway mexFunction. Even though I obtain the desired encoded bits in the created output array outPUT (I checked this via printing the array as shown below, where Output and ans should've been the same), when I call the function from the command window, I always get zeros(1,N) as the plhs[0] output, regardless of N. A result of calling the function from the command window is:
Output = 1.000000 0.000000 0.000000 1.000000
ans =
0 0 0 0
Any help would be highly appreciated and here is the whole .c code:
/*==========================================================
* mexPencoderREC.c
*
* Encodes an N = 2^n length input message to an again N-length output
* with polar coding technique, recursively.
* The calling syntax is:
* encodedOutput = mexPencoderREC(message)
*========================================================*/
#include "mex.h"
/*This function splits an array from the middle into two arrays*/
double *splitter(double *msg, int N, int whichHalf) {
double *z;
z = (double *)malloc((N/2)*sizeof(double));
int start = (whichHalf == 0) ? 0 : N/2;
for(int i = start; i<start + N/2 ; i++) {
z[i-start] = msg[i];
}
return z;
}
/*The main recursive computational routine*/
double *pencoderREC(double *msg, int N) {
double *y;
y = (double *)malloc((N)*sizeof(double));
//The Base Case
if( N==2 ) {
y[0] = (double)(((int)msg[0])^((int)msg[1]));
y[1] = msg[1];
return y;
}
//Helper variables in the process of splitting and calling recursively
double *a = pencoderREC(splitter(msg, N, 0), N/2);
double *b = pencoderREC(splitter(msg, N, 1), N/2);
//Final: Assertion of the ultimate output of the program
for(int i =0; i<N/2; i++) {
y[i] = (double)(((int)a[i])^((int)b[i]));
y[i+N/2] = b[i];
}
return y;
}
// The gateway function
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
double *msg; //The input message
int N; //Size of the message
double *outPUT; //The encoded output
N = mxGetN(prhs[0]);
msg = mxGetDoubles(prhs[0]);
//Generating the output matrix
plhs[0] = mxCreateDoubleMatrix(1,N,mxREAL);
//Getting a pointer to the real data in the output matrix
outPUT = mxGetDoubles(plhs[0]);
//Calling the computational routine
outPUT = pencoderREC(msg,N);
//Printing the encoded-bit array: outPUT
printf("Output = ");
for(int i =0; i< N; i++) {
printf("%f ",outPUT[i]);
}
printf("\n");
}

Best Answer

The problem is in these lines:
//Getting a pointer to the real data in the output matrix
outPUT = mxGetDoubles(plhs[0]);
//Calling the computational routine
outPUT = pencoderREC(msg,N);
The second line is just replacing the pointer to point somewhere else. So, plhs is still pointing at an empty double vector, while outPUT is now pointing elsewhere.
One option is to pass outPUT into pencoderREC (such that pencoderREC just fills the data into outPUT as it is generated). However, I'm not quite sure how that will work with the recursion.
Alternatively, you can leave pencoderREC unmodified, and instead add code to copy the output from pencoderREC into outPUT, instead of just replacing the pointer.
Either way, if I'm reading your code correctly, you have a pretty serious memory leak in your code. You are allocating memory in a recursive function but never freeing the memory when you are done with it. I think this is true for both splitter and pencoderREC.