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;