簡體   English   中英

為什么此MEX函數會產生意外結果?

[英]Why does this MEX function create unexpected results?

我正在學習《 加速MATLAB性能 》一書,並在第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... (即,開頭的%被錯誤地刪除了)。我將更新勘誤相應地列出。

我希望您覺得本書的其余部分有用。 如果您這樣做,那么請在Amazon上發表正面評論

printf("s() called with %d inputs,%d outputs\n",name,nrhs,nlhs);

您有3個參數,但只有兩個“%”,因此它輸出“用[name]輸入,[nrhs]輸出調用的s()”,並且不使用nlhs。 只需刪除名稱,然后改用

printf("s() called with %d inputs,%d outputs\n",nrhs,nlhs);

或使用%s顯示函數名稱:

printf("%s called with %d inputs,%d outputs\n",name,nrhs,nlhs);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM