Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET equivalent of size_t

I have a piece of .NET code which I want to port to 64-bit. The codes basically is a set of P/Invoke calls to some other C dll. One of the functions in the C dll has a parameter 'size_t'. What datatype should I use in my P/Invoke signature so that the marshalling works fine. I was thinking of using a IntPtr or a UIntPtr but some say that they are a pointer equivalent thing in .NET and shouldnt be using it. Does anyone know what the correct type for this is?

like image 713
Niranjan U Avatar asked Apr 21 '09 13:04

Niranjan U


People also ask

What is Uintptr_t?

uintptr_t is an unsigned integer type that is capable of storing a data pointer. Which typically means that it's the same size as a pointer. It is optionally defined in C++11 and later standards.

What is a size T?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.

Can Size_t be float?

Since size_t must be an unsigned type, you first need to check explicitly if your float is negative: The behaviour on converting a float that's less than or equal to -1 to an unsigned type is undefined. The second job you need to do is to check that the float is within the upper range of size_t .


1 Answers

UIntPtr will work (IntPtr would probably work too, but size_t is unsigned, so UIntPtr is a better fit)

JaredPar has written something on his blog (and a follow-up) about this.

like image 55
Patrick McDonald Avatar answered Sep 28 '22 07:09

Patrick McDonald