为 Matlab 编译 C MEX 文件时 size_t 和 mwSize 之间的差异
我目前正在致力于将 32 位 Matlab 的一些 C MEX 文件移植到 64 位 Matlab。
在这样做的过程中,我遇到了两种类型,一种来自Matlab人员,一种来自C标准。
Matlab 文档对 mwSize
的描述如下:
mwSize
(C 和 Fortran)输入尺寸值
描述
mwSize
是一种表示大小值的类型,例如数组维度。使用此功能可实现跨平台灵活性。默认情况下,mwSize
相当于 C 中的int
。使用mex -largeArrayDims
开关时,mwSize
相当于到 C 中的size_t
。在 Fortran 中,mwSize
类似地相当于INTEGER*4
或INTEGER*8
,基于关于平台和编译标志。
这是维基百科关于 size_t
的说法:
size_t
是由多个 C/C++ 标准(例如 C99 ISO/IEC 9899 标准)定义的无符号数据类型,在stddef.h
中定义。[ 1]可以通过包含stdlib.h
进一步导入它,因为该文件内部子包含stddef.h
[2]。该类型用于表示对象的大小。接受或返回大小的库函数期望它们是这种类型或具有
size_t
返回类型。此外,最常用的基于编译器的运算符sizeof
的计算结果应与size_t
兼容。
size_t
的实际类型与平台相关;一个常见的错误是假设size_t
与unsigned int
相同,这可能会导致编程错误,[3][4] 从 32 位架构迁移到 64 位架构时,例如。
据我所知,这些类型实际上是相同的。我的问题是:
- 是吗?
- 如果是的话,哪一个会被认为是更好的编程品味?理想情况下,我们希望我们的代码也与未来的 Matlab 版本兼容。我猜测答案是
mwSize
,但我不确定。
编辑:我应该补充一点,Matlab 人员正在使用两者。例如:
size_t mxGetN(const mxArray *pm);
是一个检索 mxArray 列数的函数。然而,当创建一个矩阵时,人们会使用,
mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n, mxComplexity ComplexFlag);
其中输入显然应该是 mwSize。
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 toint
in C. When using themex -largeArrayDims
switch,mwSize
is equivalent tosize_t
in C. In Fortran,mwSize
is similarly equivalent toINTEGER*4
orINTEGER*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 instddef.h
.[1] It can be further imported by inclusion ofstdlib.h
as this file internally sub includesstddef.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 operatorsizeof
should evaluate to a value that is compatible withsize_t
.The actual type of
size_t
is platform-dependent; a common mistake is to assumesize_t
is the same asunsigned 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:
- Are they?
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mwSize
是为了向后兼容性和可移植性而定义的。正如文档所述,当编译期间未使用-largeArrayDims
开关时,它映射为int
,而在编译过程中则映射为size_t
。因此,在第一种情况下,mwSize
已签名,但在第二种情况下,则没有。在代码中使用
mwSize
允许您在所有平台上重复使用代码,无论是否使用该标志。至于您指出的API不一致之处,确实是不一致,但不是主要问题。
mxGetN()
永远不会返回负数,因此让它返回size_t
就可以了。但是,(我猜测)旧版本或某些平台上的 mex API 版本期望将 int 传递给 mxCreateDoubleMatrix() ,因此将函数定义为采用 mwSize 类型的输入/code> 使其可移植和/或向后兼容。简短的答案是,使用
mwSize
并使用-largeArrayDims
编译 mex 函数。mwSize
is defined for backward compatibility and portability. As the documentation states, it maps to anint
when the-largeArrayDims
switch is not used during compilation, andsize_t
when it is. So, in the first casemwSize
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 asize_t
is OK. However, (I'm guessing) older versions or versions of the mex API on certain platforms expect an int to passed tomxCreateDoubleMatrix()
so defining the function as taking an input of typemwSize
makes it portable and / or backward compatible.Short answer is, use
mwSize
and use-largeArrayDims
to compile the mex function.