Search code examples
c++matlabmex

Matlab R2015b 64 bits Error using mex


I want to compile the following code using Matlab R2015b "

#include "mex.h"
#include "GLTree.cpp"
/* the gateway function */
//la chiamata deve essere DeleteGLtree(Tree)

void mexFunction( int nlhs,const mxArray *plhs[], 
    int nrhs, const mxArray *prhs[1]) {


//dichiarazione variabili
GLTREE* Tree;
double *ptrtree;



 if(nrhs!=1){ mexErrMsgTxt("Only one input supported.");}




ptrtree = mxGetPr(prhs[0]);//puntatore all'albero precedentemente fornito


Tree=(GLTREE*)((long)(ptrtree[0]));//ritrasformo il puntatore passato

if(Tree==NULL)
{ mexErrMsgTxt("Invalid tree pointer");
}


//chiamo il distruttore

 delete Tree;  }

but I get this error "C:\Users\Admin\Documents\MATLAB\GraphSeg\GLtree3DMex\DeleteGLTree.cpp:15:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] Tree=(GLTREE*)((long)(ptrtree[0]));"


Solution

  • You declared mexFunction incorrectly. Your declaration:

    void mexFunction( int nlhs,const mxArray *plhs[], int nrhs, const mxArray *prhs[1])
    

    is not equivalent to:

    void mexFunction( int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[])
    

    Answer to your question

    You need to drop the const before mxArray *plhs[].

    Further comments:

    You may want to check out this link on passing memory addresses back to MATLAB from a mex function. My instinct is that your casual use of a double and casting to a long (or even a long long) can be extremely problematic... It really should be a uin64, and for robustness, you may want some additional compile checks that the types all match up in that everything is 8 bytes... http://www.mathworks.com/matlabcentral/answers/75524-returning-and-passing-void-pointers-to-mex-functions