|
|
Re: mxCreateNumericArray maximal array size
Posted:
May 14, 2012 8:18 PM
|
|
"shlomi golubev" <e271p314@yahoo.com> wrote in message <jorop9$p5g$1@newscl01ah.mathworks.com>... > I have this mex code > ... > dims[0]=2*(2*n-1); dims[1]=2*(2*n-1); dims[2]=2*(2*n-1); > plhs[0]=mxCreateNumericArray(3,dims,mxSINGLE_CLASS,complexFlag); > ... > for n <= 323 it works fine, but for n >= 324 matlab crashes with an ugly core dump. > > I checked what is so special about these numbers and it appears that this is exactly the point where (2*(2*n-1))^3 becomes bigger than 2^31. > > so I run another test like this > > ... > dims[0]=2*(2*n-1); dims[1]=2*(2*n-1); > plhs[0]=mxCreateNumericArray(2,dims,mxSINGLE_CLASS,complexFlag); > ... > > again same result only this time for n <= 23170 it works fine, but for n >= 23171 matlab crashes with an ugly core dump > > so the problem is obvious, any chances to fix it soon or to provide some work around? > > thanks in advance
In a mex routine, this should never happen. If you request too much memory MATLAB is supposed to return control back to the calling routine with an "Out of memory" error. It should *not* cause an ugly core dump. The only thing I can think of is a bug in the mxCreateNumericArray routine itself ... e.g. calculating the product of the dimensions prior to a malloc call without checking to see if the product itself overflowed. I don't see anything in the doc for what is supposed to happen if any of the dims[ ] values are negative. What operating system & MATLAB version are you using?
In any event, try this workaround function that has overflow checks. Clips negative dims[ ] inputs to 0 like the zeros function.
mxArray *myCreateNumericArray(mwSize ndim, mwSize *dims, mxClassID classid, mxComplexity complexity) { mxArray *mx; mwSize i, n, d, p; mwSize *dimz; n = 1; dimz = mxMalloc( ndim * sizeof(*dimz) ); for( i=0; i<ndim; i++ ) { d = dims[i] < 0 ? 0 : dims[i]; p = n * d; if( d && (p / d) != n ) { mexErrMsgTxt("Maximum variable size allowed by the program is exceeded."); } dimz[i] = d; n = p; } mx = mxCreateNumericArray( ndim, dimz, classid, complexity ); mxFree(dimz); return mx; }
James Tursa
|
|