简体   繁体   中英

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

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# ?

PS 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;

The best equivalent for size_t in C# is the UIntPtr type. It's 32-bit on 32-bit platforms, 64-bit on 64-bit platforms, and unsigned.

You better to use nint/nuint which is wrapper around IntPtr / UIntPtr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM