Is comparing type mwSize to an int valid?

조회 수: 6 (최근 30일)
Rebecca
Rebecca 2013년 8월 14일
I'm in the process of updating mex files for 64-bit compatibility. In many places I have code where an int needs to be changed to type mwSize, but that variable still gets compared to something like an integer. For example:
mwSize n = mxGetNumberOfElements(mxArray);
int_T i;
and then the following code:
for (i = 1; i < n; i++)
...other stuff
Here "i" is of type int_T and "n" is of type mwSize. Is this a valid comparison? I don't get any warnings when I compile the 64-bit mex here, but I was wondering if this could cause any problems when I execute the mex?
Thanks,
Rebecca

채택된 답변

James Tursa
James Tursa 2013년 8월 14일
편집: James Tursa 2013년 8월 14일
If int_T is a 32-bit integer and mwSize is a 64-bit integer (typically size_t, unsigned) then yes it is physically possible to have aproblem.
E.g., if n happens to be bigger than the largest value for an int_T then you would have an infinite loop because the i++ would typically wrap around to a negative value when it overflowed (not required for the compiler to do this, but typically that is what happens). Whether mxGetNumberOfElements could ever return a number this big is another question, and may be dependent on the particular system you are running on.
The i < n test itself may have a problem if int_T is a signed 64-bit and mwSize is an unsigned 64-bit and n happens to be larger than the largest int_T value. Again you would have an infinite loop for the same reason (i < n is always true). This case is not a practical issue, however, because the massive memory that such a value would imply would not be physically possible.
So, I would guess your code will probably work as long as the variable sizes are not too large.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by