什么等于c#中的c++ size_t

what is equal to the c++ size_t in c#

我有一个 C++ 结构体:

struct some_struct{
uchar* data;
size_t size;
}

我想在manged(c#) 和native(c++) 之间传递它。 C#size_t 的等价物是什么?

P.S。我需要大小完全匹配,因为任何字节差异都会在包装时导致巨大的问题

编辑:

本机代码和托管代码都在我的完全控制之下(我可以随意编辑)

C# 中 size_t 的最佳等价物是 UIntPtr 类型。它在 32 位平台上是 32 位的,在 64 位平台上是 64 位的,并且是无符号的。

没有等同于 size_t 的 C#。

无论平台如何,C# sizeof() operator 总是 returns 一个 int 值,因此从技术上讲,size_t 的 C# 等价物是 int,但这没有帮助给你。

(请注意 Marshal.SizeOf() 也是 returns 和 int。)

另请注意,就 sizeof()Marshal.Sizeof() 而言,任何 C# 对象的大小都不能超过 2GB。数组可以大于 2GB,但不能对数组使用 sizeof()Marshal.SizeOf()

为了您的目的,您将需要知道 DLL 中的代码版本用于 size_t 并在 C# 中使用适当大小的整数类型。

需要认识到的一件重要事情是,在 C/C++ 中,size_t 通常具有与 intptr_t 相同的位数,但这并不能保证,尤其是对于分段架构。

我知道很多人都说 "use UIntPtr",这通常会起作用,但不能保证是正确的。


来自C/C++ definition of size_tsize_t

is the unsigned integer type of the result of the sizeof operator;

你最好使用nint/nuint which is wrapper around IntPtr/UIntPtr