什么等于c#中的c++ size_t [英] what is equal to the c++ size_t in c#

查看:26
本文介绍了什么等于c#中的c++ size_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C++ 中有一个结构体:

struct some_struct{uchar* 数据;size_t 大小;}

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

附言我需要大小完全匹配,因为在包装

时任何字节差异都会导致巨大的问题

本机和托管代码都在我的完全控制之下(我可以编辑任何我想要的)

解决方案

没有与 size_t 等效的 C#.

C# sizeof() 运算符 无论平台如何,总是返回 int 值,因此从技术上讲,size_t 的 C# 等价物是 int,但这对您没有帮助.

(注意Marshal.SizeOf() 也返回一个 int.)

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

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

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

我知道很多人说使用 UIntPtr",这通常会起作用,但不保证正确.

<小时>

来自 C/C++ 定义 size_t, size_t

<块引用>

为sizeof运算符结果的无符号整数类型;

I have a struct in c++:

struct some_struct{
uchar* data;
size_t size;
}

I want to pass it between manged(c#) and native(c++). What is the equivalent of size_t in C# ?

P.S. I need an exact match in the size because any byte difference will results in huge problem while wrapping

EDIT:

Both native and manged code are under my full control ( I can edit whatever I want)

解决方案

There is no C# equivalent to size_t.

The C# sizeof() operator always returns an int value regardless of platform, so technically the C# equivalent of size_t is int, but that's no help to you.

(Note that Marshal.SizeOf() also returns an int.)

Also note that no C# object can be larger than 2GB in size as far as sizeof() and Marshal.Sizeof() is concerned. Arrays can be larger than 2GB, but you cannot use sizeof() or Marshal.SizeOf() with arrays.

For your purposes, you will need to know what the version of code in the DLL uses for size_t and use the appropriate size integral type in C#.

One important thing to realise is that in C/C++ size_t will generally have the same number of bits as intptr_t but this is NOT guaranteed, especially for segmented architectures.

I know lots of people say "use UIntPtr", and that will normally work, but it's not GUARANTEED to be correct.


From the C/C++ definition of size_t, size_t

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

这篇关于什么等于c#中的c++ size_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆