c++ - 为什么这个 MEX 函数会产生意想不到的结果?

标签 c++ matlab mex

我正在学习这本书Accelerating MATLAB Performancepage 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/

相关文章:

c++ - sqlite 表代码管理器?

c++ - 排序元素,但保持某些元素固定

MATLAB Function 模块输出作为虚拟总线

matlab - 从用于多类分类的标签生成向量矩阵(向量化)

c++ - mex 编译错误 C2440 : 'initializing' : cannot convert from 'const mwSize *' to 'const int32_t *'

c++ - 编译cpp文件时代码不并行,c是并行的

c++ - 为什么现有函数参数不能用于评估其他默认参数?

c++ - 添加元素后链表程序崩溃

matlab - 正弦波的相位谱与Matlab

matlab - 确定Matlab中的编译器