为 Matlab 编译 C MEX 文件时 size_t 和 mwSize 之间的差异

发布于 2024-11-18 22:41:30 字数 1534 浏览 5 评论 0原文

我目前正在致力于将 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*4INTEGER*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_tunsigned int 相同,这可能会导致编程错误,[3][4] 从 32 位架构迁移到 64 位架构时,例如。

据我所知,这些类型实际上是相同的。我的问题是:

  1. 是吗?
  2. 如果是的话,哪一个会被认为是更好的编程品味?理想情况下,我们希望我们的代码也与未来的 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-11-25 22:41:30

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文