10

I am currently working on porting some C MEX-files for 32-bit Matlab to 64-bit Matlab.

While doing so, I have encountered two types, one coming from the Matlab people, and one which is C standard.

This is what the Matlab documentation is saying about mwSize:

mwSize (C and Fortran)

Type for size values

Description

mwSize is a type that represents size values, such as array dimensions. Use this function for cross-platform flexibility. By default, mwSize is equivalent to int in C. When using the mex -largeArrayDims switch, mwSize is equivalent to size_t in C. In Fortran, mwSize is similarly equivalent to INTEGER*4 or INTEGER*8, based on platform and compilation flags.

This is what Wikipedia is saying about size_t:

size_t is an unsigned data type defined by several C/C++ standards (e.g., the C99 ISO/IEC 9899 standard) that is defined in stddef.h.[1] It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h[2].

This type is used to represent the size of an object. Library functions that take or return sizes expect them to be of this type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a value that is compatible with size_t.

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors,[3][4] when moving from 32 to 64-bit architecture, for example.

As far as I can see, these types are actually the same. My questions are:

  1. Are they?
  2. If they are, which one would be considered better programming taste to use? Ideally we would like our code to be compatible with future Matlab releases as well. I am guessing that the answer is mwSize, but I am not sure.

Edit: I should add that the Matlab people are using both. For example:

size_t mxGetN(const mxArray *pm);

is a function that is retrieving the number of columns of an mxArray. However, when one creates a matrix, one uses,

mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n, mxComplexity ComplexFlag);

where the input evidently should be mwSize.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Har
  • 377
  • 2
  • 13
  • 1
    Hmmm ... as a basic check just make sure they have the same size: `assert(sizeof (size_t) == sizeof (mwSize));` and use any one of them. If the `assert` fails you're doomed. – pmg Jul 05 '11 at 15:33
  • 3
    @pmg: The assert will fail on a platform where `size_t` is larger than an `int` if you do not use the `-largeArrayDims` switch when compiling your mex function. That is the whole point of having the `mwSize` typedef. – Praetorian Jul 05 '11 at 16:52
  • +1 and thanks @Praetorian. As I said in the comment, the `assert` is meant as only a basic check: I don't know mex specific stuff. – pmg Jul 05 '11 at 17:20

1 Answers1

12

mwSize is defined for backward compatibility and portability. As the documentation states, it maps to an int when the -largeArrayDims switch is not used during compilation, and size_t when it is. So, in the first case mwSize is signed, but in the second, it isn't.

Using mwSize in your code allows you to re-use the code on all platforms, irrespective of whether that flag is used or not.

As for the API inconsistencies you've pointed out, they are truly inconsistencies, but not ones for major concern. mxGetN() will never return a negative number, so having it return a size_t is OK. However, (I'm guessing) older versions or versions of the mex API on certain platforms expect an int to passed to mxCreateDoubleMatrix() so defining the function as taking an input of type mwSize makes it portable and / or backward compatible.

Short answer is, use mwSize and use -largeArrayDims to compile the mex function.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 1
    Another question on style: mwIndex and mwSize are (in the documentation) identical, except that one is for the index and one is for the size. What is the reason for this? Is it just to make code clearer? So in `for(i=0; i<=n; i++)`, i would be mwIndex and n would be mwSize, but to me the division seems unneccessary. – Har Jul 06 '11 at 09:46
  • @Har: I've never understood the reason for having mwIndex either. It does make the code easier to follow in some cases, but as you've pointed out, when iterating over some dimension of an mxArray you end up with an mwIndex compared to an mwSize and I don't like that either. I tend to use mwSize for everything. – Praetorian Jul 06 '11 at 12:34