Matlab R2015b 64 位错误使用 mex

Matlab R2015b 64 bits Error using mex

我想使用 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;  }

但我收到此错误“C:\Users\Admin\Documents\MATLAB\GraphSeg\GLtree3DMex\DeleteGLTree.cpp:15:38: 警告:从不同大小的整数转换为指针 [-Wint-to-pointer-cast] 树=(GLTREE*)((long)(ptrtree[0]));

这是基于你的代码的猜测(我没有编译它):在 64 位机器上,地址 space 有大小为 8 字节(64 位)的指针,你投射了一个指针键入 long 可能只有 4 个字节长。如果你想转换你应该使用一个 8 字节长的类型,比如 long long (保证至少是 8 个字节)

您错误地声明了 mexFunction。您的声明:

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

不等同于:

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

回答你的问题

您需要在 mxArray *plhs[] 之前删除 const

进一步评论:

您可能想查看此 link 关于将内存地址从 mex 函数传回 MATLAB 的内容。我的直觉是,随意使用 double 并转换为 long(或什至 long long)可能会非常有问题......它确实应该是 uin64,并且为了稳健性,你可能需要一些额外的编译检查类型一切都匹配,因为一切都是 8 个字节...... http://www.mathworks.com/matlabcentral/answers/75524-returning-and-passing-void-pointers-to-mex-functions