Is comparing type mwSize to an int valid?

6 vues (au cours des 30 derniers jours)
Rebecca
Rebecca le 14 Août 2013
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

Réponse acceptée

James Tursa
James Tursa le 14 Août 2013
Modifié(e) : James Tursa le 14 Août 2013
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.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by