Search code examples
matlabmemory-managementmexrealloc

mex memory allocation mxRealloc


I have to read a matrix of double, handling its values and insert them in a new matrix, whose one of its dimension is unkwown at the beginning.

In a static memory allocation, my code is:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  int out_cols = numsubmtx;
  mxArray *out = mxCreateDoubleMatrix( out_rows, out_cols, mxREAL );
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = 
                                            indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }}}
  plhs[0] = out;
}

In a dynamic version, I do not know the value of out_cols, so I have to reallocate *out when a condition on the values of indata is satisfied.

My idea is something like this:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  //int out_cols = numsubmtx; NOW UNKNOWN!
  mxArray *out = NULL;
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    double condition=0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      condition += indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ]/(xp*yp*zp);
                                      ++row;
                                }}}
                    if(coundition>0.5){
                      out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
                      double *outdata = mxGetPr(out);
                      int row = 0;
                      for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }
              }}}
  plhs[0] = out;
}

What is wrong?


Solution

  • I haven't looked closely at the logic of your code, but I can see the following problems:

    mxArray *out = NULL;
    double *outdata = mxGetPr(out);
    

    and

    out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
    

    will have strange behavior. An mxArray is a structure, and you want to reallocate memory for the data pointed to by the structure rather than the structure itself. Instead, try

    double *outdata = mxMalloc(out_rows*sizeof(double));
    

    and

    outdata = mxRealloc(outdata, (submtx_counter+1)*out_rows*sizeof(double));
    

    and then at the very end, create your output matrix:

    mxArray *out = mxCreateDoubleMatrix(out_rows, submtx_counter, mxREAL);
    mxSetPr(out, outdata);
    plhs[0] = out;
    

    This will guarantee that the metadata in out gives you a matrix of the correct size, etc.