Difference between size_t and mwSize when MEXing C with Matlab

I'm currently working on porting some C files that fit in 32-bit Matlab to 64-bit Matlab.

In doing so, I came across two types: one of the Matlab people and one that is the C standard.

This is what the Matlab documentation says about mwSize:

mwSize (C and Fortran)

Type for size values

Description

mwSize is a type that represents size values, such as array sizes. Use this feature 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 equivalent to INTEGER * 4 or INTEGER * 8 based on platform and compilation flags.

This is what Wikipedia says about size_t:

size_t is an unsigned data type defined by several C / C ++ standards (for example, the C99 ISO / IEC 9899 standard), which is defined in stddef.h. [1] It can be further imported by including stdlib.h, since this internal file includes stddef.h [2].

This type is used to represent the size of an object. Library functions that accept or return sizes expect them to be of this type or have a return_type of size_t. In addition, the most commonly used size size operator for the compiler should evaluate to a value compatible with size_t.

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

As far as I can see, these types are actually the same. My questions are: 1) They? 2) If so, which one will be considered the best programmatic taste for use. Ideally, we would like our code to be compatible with future Matlab releases. I assume the answer will be mwSize, but I'm not sure.

Edit: I have to add that Matlab people use both. For example,

size_t mxGetN(const mxArray *pm); 

is a function that returns the number of mxArray columns. However, when you create a matrix, we use

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

where the input should obviously be mwSize.

+7
source share
1 answer

mwSize defined for backward compatibility and portability. As stated in the documentation, it maps to int when the -largeArrayDims switch -largeArrayDims not used at compile time and size_t when it is. So, in the first case, mwSize signed, and in the second - not.

Using mwSize in your code allows you to reuse code on all platforms, regardless of whether this flag is used or not.

Regarding the API inconsistencies you mentioned, they are really inconsistent, but not for serious issues. mxGetN() will never return a negative number, so when returning size_t is fine. However (I assume) older versions or mex API versions on some platforms expect int to be passed to mxCreateDoubleMatrix() , so defining a function as input input of type mwSize makes it portable and / or backward compatible.

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

+9
source

All Articles