我正在学习这本书Accelerating MATLAB Performance和 page 394 ,这段代码是这样写的:
#include "mex.h"
void mexFunction (int nlhs,mxArray *plhs[],/*outputs*/
int nrhs, const mxArray *prhs[])/*inputs*/
{
const char *name = mexFunctionName();
printf("s() called with %d inputs,%d outputs\n",name,nrhs,nlhs);
}
根据书上所说,用命令mex hello.cpp
构建MEX代码后,应该会产生如下结果:
>> hello
hello() called with 0 inputs, 0 outputs
>> hello(1,2,3)
hello() called with 3 inputs, 0 outputs
>> [a,b] = hello(1,2,3)
hello() called with 3 inputs, 2 outputs
One or more output arguments not assigned during call to "hello".
但是当我在我的Win7x64
机器上运行相同的代码时,结果如下:
>> mex hello.cpp
Building with 'Microsoft Visual C++ 2010'.
MEX completed successfully.
>> hello
s() called with 2082650752 inputs,0 outputs
>> hello(1,2,3)
s() called with 2082650752 inputs,3 outputs
>> [a,b] = hello(1,2,3)
s() called with 2082650752 inputs,3 outputs
One or more output arguments not assigned during call to "hello".
这些意外结果的原因是什么?
最佳答案
感谢您提醒我 - 这是本书编辑阶段输入的错字 - 正确的代码是 printf('%s() called...
(即前导 %
被错误删除)。我将相应地更新勘误表。
我希望本书的其余部分对您有所帮助。如果你这样做,那么请post a positive comment on Amazon .
关于c++ - 为什么这个 MEX 函数会产生意想不到的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972760/