2015-07-10 36 views
2

假设一个MEX函数被调用这样传递给mex函数的数据是否保证了非别名?

ret = aMexFunction(foo, foo); % same data for both inputs 

而在MEX功能:定义为

void mexFunction(int nlhs, mxArray *plhs[], 
        int nrhs, const mxArray *prhs[]) 
{ 
    bar(mxGetData(prhs[0]), mxGetData(prhs[1])); 
} 

bar

bar(Type *restrict ptr1, Type *restrict ptr2) 
{   ^^^^^^^^    ^^^^^^^^ // could this be a problem? 
    doSomeThing(); 
} 
+0

详细说明在这些论点中发生的情况吧? – this

+0

@this bar采用'restrict'ed指针,所以如果'mxGetData(prhs [0])'和'mxGetData(prhs [0])'返回相同的底层存储位置,则它是UB。 – user3528438

+1

@Praetorian http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing根据这个链接,Matlab在传递给mex函数时不会重复数据。而不是只写别名的“限制”指针,从别名“限制”指针读取也会导致不好的结果。 – user3528438

回答

0

你只需要有限的指针时,指针不是const,因为只有当写入时别名才会成为问题。 mex函数的输入是const,并且该constability应该传递给调用函数。输入数据数组不能保证指向非混淆的内存位置,因为MATLAB假定输入不会改变,因为它们不应该是这样。

相关问题