1 // Compiler options: -unsafe
5 class PointerArithmeticTest
7 unsafe static int Main()
10 return CheckAdd((byte*)(-1), -1);
11 } catch (System
.OverflowException
) {}
14 return CheckSub((short*)(-1), int.MaxValue
);
15 } catch (System
.OverflowException
) {}
17 CheckSub2((short*)(-1), int.MaxValue
);
19 if ((long)Conversions (long.MaxValue
) != (IntPtr
.Size
<= 4 ? uint.MaxValue
: long.MaxValue
))
22 Console
.WriteLine ("OK");
26 unsafe static int* Conversions (long b
)
31 unsafe static int CheckAdd(byte* ptr
, int offset
)
33 if (checked(ptr
+ offset
< ptr
))
39 unsafe static int CheckSub(short* ptr
, int offset
)
41 if (checked(ptr
- offset
< ptr
))
47 unsafe static int CheckSub2(short* ptr
, int offset
)
49 short* b
= ptr
+ offset
;
50 if (checked(ptr
- b
< 0))