Reputation: 524
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]));"
Upvotes: 0
Views: 1071
Reputation: 4529
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[])
You need to drop the const
before mxArray *plhs[]
.
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
Upvotes: 4
Reputation: 9655
This is a guess based on your code (I didn't compile it): On 64 bit machine, the address space has pointers of size 8 bytes (64 bit), and you cast a pointer to type long
which is probably only 4 bytes long. If you want to cast you should use an 8 byte long type, such as long long
(which is guaranteed to be at least 8 bytes)
Upvotes: 1