14

I was looking at the P/Invoke declaration of RegOpenKeyEx when I noticed this comment on the page:

Changed IntPtr to UIntPtr: When invoking with IntPtr for the handles, you will run into an Overflow. UIntPtr is the right choice if you wish this to work correctly on 32 and 64 bit platforms.

This doesn't make much sense to me: both IntPtr and UIntPtr are supposed to represent pointers so their size should match the bitness of the OS - either 32 bits or 64 bits. Since these are not numbers but pointers, their signed numeric values shouldn't matter, only the bits that represent the address they point to. I cannot think of any reason why there would be a difference between these two but this comment made me uncertain.

Is there a specific reason to use UIntPtr instead of IntPtr? According to the documentation:

The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the IntPtr type is used in the common language runtime. The UIntPtr type is provided mostly to maintain architectural symmetry with the IntPtr type.

This, of course, implies that there's no difference (as long as someone doesn't try to convert the values to integers). So is the above comment from pinvoke.net incorrect?

Edit:

After reading MarkH's answer, I did a bit of checking and found out that .NET applications are not large address aware and can only handle a 2GB virtual address space when compiled in 32-bit mode. (One can use a hack to turn on the large address aware flag but MarkH's answer shows that checks inside the .NET Framework will break things because the address space is assumed to be only 2GB, not 3GB.)

This means that all correct virtual memory addresses a pointer can have (as far as the .NET Framework is concerned) will be between 0x00000000 and 0x7FFFFFFF. When this range is translated to signed int, no values would be negative because the highest bit is not set. This reinforces my belief that there's no difference in using IntPtr vs UIntPtr. Is my reasoning correct?

Fermat2357 pointed out that the above edit is wrong.

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • Take a look also here. http://stackoverflow.com/questions/1320296/intptr-vs-uintptr – David J Nov 01 '12 at 16:29
  • 1
    @Fermat2357 Yes, that was the question that triggered me typing up a new question. – xxbbcc Nov 01 '12 at 16:40
  • I think you are correct. There is no difference at all. A `HANDLE` is simply a unique number to a resource (of course in Win32 it is often implemented as a 32 bit pointer to a memory area). However, `IntPtr` is able to embed any kind of 32 bit number. The difference to UIntPtr is only the interpretation of this number nothing more. – David J Nov 02 '12 at 02:47

3 Answers3

11

UIntPtr and IntPtr are internal implemented as

private unsafe void* m_value;

You are right both simply only managing the bits that represent a address.

The only thing where I can think about an overflow issue is if you try to perform pointer arithmetics. Both classes support adding and subtracting of offsets. But also in this case the binary representation should be ok after such an operation.

From my experience I would also prefer UIntPtr, because I think on a pointer as an unsigned object. But this is not relevant and only my opinion.

It seems not make any difference if you use IntPtr or UIntPtr in your case.

EDIT:

IntPtr is CLS-compliant because there are languages on top of the CLR which not support unsigned.

David J
  • 1,554
  • 9
  • 15
  • 7
    I find this kind of framework design that Microsoft did very unsettling - having a duplicate meaningless type to wrap pointers for the sake of symmetry. They should've just created one wrapper and called it SafePtr and not go into the signed/unsigned territory which makes no sense at all. – xxbbcc Nov 02 '12 at 13:16
4

This, of course, implies that there's no difference (as long as someone doesn't try to convert the values to integers).

Unfortunately, the framework attempts to do precisely this (when compiled specifically for x86). Both the IntPtr(long) constructor, and the ToInt32() methods attempt to cast the value to an int in a checked expression. Here's the implementation seen if using the framework debugging symbols.

    public unsafe IntPtr(long value)
    { 
        #if WIN32
            m_value = (void *)checked((int)value);
        #else
            m_value = (void *)value; 
        #endif
    } 

Of course, the checked expression will throw the exception if the value is out of bounds. The UIntPtr doesn't overflow for the same value, because it attempts to cast to uint instead.

Mark H
  • 13,797
  • 4
  • 31
  • 45
  • I wonder if that check is in place because user virtual address space goes from 0x00000000 to 0x7FFFFFFF. Since bit 31 is not set in a valid virtual memory address, the `checked` conversion shouldn't fail on a 32-bit OS. I'm not sure if .NET supports /3GB, since that extends the virtual address space. – xxbbcc Nov 01 '12 at 14:14
  • There are two ctors. One `public IntPtr(long value)` and the second `public IntPtr(int value)`. Its completely normal (for 32 bit windows) that the first ctor only accepts 64 bit values which can be casted to 32 bit without lost of precision. – David J Nov 01 '12 at 14:48
  • @Fermat2357 yes, that's right but the `checked` conversion is not about loss of precision, it's about validating the address to be within the valid address range. In theory that check shouldn't fail on 32-bit systems because the highest bit shouldn't be set in user space. – xxbbcc Nov 01 '12 at 15:34
  • I just checked, .NET applications are not large address aware (one can only hack in the flag using `editbin` so I think the above `checked` conversion should never fail on a 32-bit system, even if the OS runs with `/3GB` - since the application is not large address aware, it will get the regular 2GB virtual address space. – xxbbcc Nov 01 '12 at 15:39
  • _The `UIntPtr` doesn't overflow for the same value, because it attempts to cast to `uint` instead._ This is not a problem, except maybe if you use the `ctor` with a `long` not fit into an `int`. By the way `UIntPtr`can overflow too. And its implemented the same way with a `checked` statement like in the signed case. – David J Nov 02 '12 at 02:50
  • @xxbbcc _yes, that's right but the checked conversion is not about loss of precision, it's about validating the address to be within the valid address range._ No, I disagree on that. `IntPtr` can hold any valid 32 Bit number even those over `0x7FFFFFFF`. Please take a look at how such values are casted to `long`. To overcome this confusing you can simply using the `ctor` with `int`. – David J Nov 02 '12 at 05:52
  • @Fermat2357 You're right - I overlooked the `int` constructor. I guess the `checked` conversion _is_ about loss of precision then. – xxbbcc Nov 02 '12 at 13:12
0

The difference between IntPtr\UIntPtr is the same as the differences found between: Int32\UInt32 (ie, it's all about how the numbers get interpreted.)

Usually, it doesn't matter which you choose, but, as mentioned, in some cases, it can come back to bite you.

(I'm not sure why MS chose IntPtr to begin with(CLS Compliance, etc,.), memory is handled as a DWORD(u32), meaning it's unsigned, thus, the preferred method should be UIntPtr, not IntPtr, right?)

Even: UIntPtr.Add()

Seems wrong to me, it takes a UIntPtr as a pointer, and an 'int' for the offset. (When, to me, 'uint' would make much more sense. Why feed a signed value to an unsigned method, when, more than likely the code cast it to 'uint' under the hood. /facepalm)

I would personally prefer UIntPtr over IntPtr simply because the unsigned values match the values of the underlying memory which I'm working with. :)


Btw, I'll likely end up creating my own pointer type(using UInt32), built specifically for working directly with memory. (I'm guessing that UIntPtr isn't going to catch all the possible bad memory issues, ie, 0xBADF00D, etc, etc,. Which is a CTD waiting to happen... I'll have to see how the built in type handles things first, hopefully, a null\zero check is properly filtering out stuff like this.)

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Smoke
  • 21
  • 1