IntPtr的除 [英] IntPtr addition
问题描述
因此,从我可以告诉,每个托管例如IntPtr的另外我发现的是错误
So from what I can tell, every managed example of IntPtr addition I have found is WRONG.
例如: http://www.atalasoft.com/cs /blogs/stevehawley/archive/2006/10/16/10987.aspx
我的想法之中,如果IntPtr的是(或接近)int32.MaxValue在32位系统上,又添加了一个偏移量溢出INT32,是不是仍然是一个有效的内存地址(因为这将是有效的在UINT32,并会在IntPtr的负数psented重新$ P $)?!
My thought being, that if IntPtr is at (or near) int32.MaxValue on a 32-bit system, and you add an offset which overflows int32, isn't that still a valid memory address (as it would be valid in uint32, and would be represented by a negative number in IntPtr)?!
我相信,code应该是这样的:
I believe the code should be something like:
public static IntPtr Offset(IntPtr src, int offset)
{
switch (IntPtr.Size) {
case 4:
return new IntPtr((int)((uint)src + offset));
case 8:
return new IntPtr((long)((ulong)src + offset));
default:
throw new NotSupportedException("Not supported");
}
}
我是疯了吗?
Am I crazy?
有没有人有一个尝试和真正的IntPtr另外的例子吗?
Does anyone have a tried and true IntPtr addition example?
推荐答案
我觉得一点是,如果你溢出一个int,你仍然可以得到相应的价值。试试这个:
I think the point is that if you overflow an int, you still get the appropriate value. Try this:
//-2147483645
Console.WriteLine( int.MaxValue + 4 );
//2147483651
Console.WriteLine( (uint)(int.MaxValue + 4) );
由于int.MaxValue是2147483647,铸造溢出负数为uint事实上确实给出正确的值。
Given that int.MaxValue is 2147483647, casting the overflowed negative number to uint does in fact give the right value.
这篇关于IntPtr的除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!