C Mex crashes if array is too large

2 ビュー (過去 30 日間)
JohnDapper
JohnDapper 2016 年 4 月 3 日
編集済み: James Tursa 2016 年 4 月 5 日
Hi all,
I am passing an array to a mex file as follows:
float *var;
var = (float*)mxGetPr(prhs[0]);
The mex file compiles/work perfect when the input is "small" (a few GB for my application), but when the input is larger, it crashes and gives me a segmentation fault. The input is held in memory (loaded into matlab workspace), and furthermore, the mex file shouldn't make a copy of the array (I'm just pointing to the array which is already in memory). Not only that, but I only need to access few elements of the input array at a time.
Any ideas on what could be causing this behavior?
One obvious candidate: I'm not compiling with -largeArrayDims (didn't update my code for 64-bit API). Might this be an issue?
  3 件のコメント
JohnDapper
JohnDapper 2016 年 4 月 5 日
Thank you James and Dave,
The problem had to do with using a 32-bit int type index when accessing the array "var" (i.e. x = var(index)). When I cast the index to mwSize (and compile with -largeArrayDims), the problem vanished. I'm assuming this is because the index variable was being assigned a value larger than the maximum allowed with a 32-bit int.
James Tursa
James Tursa 2016 年 4 月 5 日
編集済み: James Tursa 2016 年 4 月 5 日
Likely. -largeArrayDims controls whether mwSize is an int or a size_t. If you were using an int for the index and it wrapped around to a negative value then of course you would be pointing to invalid memory hence a crash. Keep in mind, however, that since mwSize is now likely a size_t in your code you need to be careful how you are doing your if-testing since size_t is unsigned. Are you actually "casting" an int to a size_t? I assume you meant typing the variable as a mwSize, not casting.

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

回答 (1 件)

Dave Behera
Dave Behera 2016 年 4 月 5 日
I think you should definitely try recompiling with -largeArrayDims. Also, paste a snippet of your code here.

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by