在使用Matlab的情况下,使用的是size_t和mwSize的差值。

时间:2022-11-13 16:09:14

I am currently working on porting some C files which are mexed with 32 bit Matlab to 64 bit Matlab.

我目前正在进行一些C文件的移植,这些文件是用32位的Matlab到64位的Matlab进行的。

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

在这样做的过程中,我遇到了两种类型,一种来自于Matlab人员,另一种是C标准。

This is what the Matlab documentation is saying about mwSize:

这是Matlab文档中关于mwSize的说明:

mwSize (C and Fortran)

mwSize(C和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.

mwSize是表示大小值的类型,例如数组维数。使用此功能可实现跨平台的灵活性。默认情况下,mwSize在c中相当于int .当使用mex -largeArrayDims开关时,mwSize与c中的size_t等效,在Fortran中,mwSize类似于整数*4或整数*8,基于平台和编译标志。

This is what Wikipedia is saying about size_t:

这就是*所说的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].

size_t是一个未签名的数据类型,由几个C/ c++标准(例如,C99 ISO/IEC 9899标准)定义,在stddef.h中定义。[1]可以通过包含stdlib进一步导入。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.

该类型用于表示对象的大小。获取或返回大小的库函数期望它们是这种类型的,或者具有size_t的返回类型。此外,最常用的基于编译器的运算符sizeof应该评估一个与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.

实际类型的size_t是平台相关的;一个常见的错误是假设size_t与unsigned int相同,这可能导致编程错误,[3][4]在从32位架构迁移到64位架构时,例如。

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.

据我所知,这些类型实际上是一样的。我的问题是:1)是吗?2)如果是的话,那将被认为是更好的编程品味。理想情况下,我们希望我们的代码与未来的Matlab版本兼容。我猜答案是mwSize,但我不确定。

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

编辑:我要补充的是,Matlab的人都在使用这两种方法。例如,

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的列数的函数。然而,当一个人创造了一个矩阵,

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

where the input evidently should be mwSize.

输入显然应该是mwSize。

1 个解决方案

#1


9  

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.

mwSize定义为向后兼容和可移植性。作为文档状态,在编译时不使用-largeArrayDims开关时,它会映射到一个int类型,而当它是时,则映射到size_t。所以,在第一种情况下,mwSize是有符号的,但是在第二种情况下,它不是。

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

在代码中使用mwSize允许您在所有平台上重用代码,而不考虑是否使用该标志。

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.

至于你所指出的API不一致性,它们确实是不一致的,但不是主要问题。mxGetN()永远不会返回一个负数,因此返回一个size_t是可以的。但是,(我猜测)在某些平台上的mex API的旧版本或版本期望一个int传递给mxCreateDoubleMatrix(),因此定义该函数作为输入类型mwSize使其可移植和/或向后兼容。

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

简短的回答是,使用mwSize并使用-largeArrayDims来编译mex函数。

#1


9  

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.

mwSize定义为向后兼容和可移植性。作为文档状态,在编译时不使用-largeArrayDims开关时,它会映射到一个int类型,而当它是时,则映射到size_t。所以,在第一种情况下,mwSize是有符号的,但是在第二种情况下,它不是。

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

在代码中使用mwSize允许您在所有平台上重用代码,而不考虑是否使用该标志。

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.

至于你所指出的API不一致性,它们确实是不一致的,但不是主要问题。mxGetN()永远不会返回一个负数,因此返回一个size_t是可以的。但是,(我猜测)在某些平台上的mex API的旧版本或版本期望一个int传递给mxCreateDoubleMatrix(),因此定义该函数作为输入类型mwSize使其可移植和/或向后兼容。

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

简短的回答是,使用mwSize并使用-largeArrayDims来编译mex函数。