本文介绍了Matlab是否曾经复制过传递给mex函数的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于延迟复制:Matlab是否会复制传递给mexFunction的数据进行修改吗?

Concerning lazy copying: Will Matlab ever copy data passed to a mexFunction, which modifies it?

例如

myMex(input1(:,:,ii), input2(:,:,ii))

可以确定输入矩阵永远不会被复制,这样就可以传递某些内容并对其进行修改,而不必返回引用?

can one be sure, that the input matrices are never copied, so that one can pass something in and modify it, without having to return a reference?

推荐答案

在某些情况下,当调用函数.

使用MEX函数时,输入作为const mxArray *prhs[]传递(prhs是指向常量数据的指针数组).即使可以在不制作副本的情况下更改输入变量(通过消除常量性),也会危险,并且没有得到官方的支持,并且可能会产生意想不到的结果,甚至产生段错误(使用写时复制技术).官方的答案是复制输入数组,然后返回修改后的数组.

With MEX-functions, the input as passed as const mxArray *prhs[] (prhs is an array of pointers to constant data). Even though it is possible to change input variables without making copies (by casting away the constant-ness), it is dangerous and not officially supported, and could yield unexpected results and even segfaults (on the account of the copy-on-write technique). The official answer is to duplicate the input array, and return the modified array.

如果您愿意使用未记录的功能,请参见 mxUnshareArray 等.这是一个文章,对此进行了详细说明.

If you are willing to use undocumented features, see the mxUnshareArray and the like.. Here is an article by Yair Altman that explains this in more details.

这篇关于Matlab是否曾经复制过传递给mex函数的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:00