mex debugging, integer division cause crash?

1 回表示 (過去 30 日間)
Knut
Knut 2012 年 9 月 12 日
Obviously, I am doing something wrong, and my code is too complex to repeat here. I have tried to distill the parts that seems pertinent. Anyone have any good ideas?
static void myfunc(unsigned char arr[], int n, int m) {
uint64_t nom = 255;
uint64_t den = arr[0];
uint64_t y = nom/den;//cause crashing
//uint64_t y = nom*den;//no problem
//uint64_t y = nom/42;//no problem
}
Windows platform:
Caught MathWorks::System::FatalException
OSX:
"MATLAB has encountered an internal problem and needs to close" (floating point exception detected)
The stack trace suggests my mex file calling libSystem.B.dylib, libmwmcr.dylib, libmwfl.dylib.
I am confused by the fact that I am able to "touch" the variable when doing multiplication, but as soon as I do a division, MATLAB seems to think that there is a floating point exception??
  2 件のコメント
Knut
Knut 2012 年 9 月 13 日
Problem solved.
My MATLAB code relied on this behaviour:
>>uint8(1)/uint8(0)
>>ans =
>>
>>255
When the code was directly ported to C, an integer division of zero cause an exception to be raised. Different C compilers and MATLAB versions (?) seems to handle this differently, and (dare I say) somewhat confusingly.
Jan
Jan 2012 年 9 月 13 日
A floating point exception for an integer division sound strange. This is another good excample for the advantages of Matlab compared to C/C++: Matlab behaves equally on different platforms, while the basic calculations depend critically on the compiler.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2012 年 9 月 12 日
This seem magic. And whenever magic things happen in the computer, they are deterministic effects of things, which happen somewhere else.
Please try to run your code from a clean and bug free main function. If and only if this is still crashing, I'm convinved that the bug is inside myfunc().

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by