Is comparing type mwSize to an int valid?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!