Fix PowerPC regression caused by tracing changes (#11907)
[mono-project.git] / mcs / docs / ecma334 / 25.5.6.xml
bloba86fcd19d649564a3fb419cdccfefbd388f1ca8b
1 <?xml version="1.0"?>
2 <clause number="25.5.6" title="Pointer arithmetic">
3   <paragraph>In an unsafe context, the + operator (<hyperlink>14.7.4</hyperlink>) and  -operator (<hyperlink>14.7.5</hyperlink>) can be applied to values of all pointer types except void*. Thus, for every pointer type T*, the following operators are implicitly defined: <code_example><![CDATA[
4 T* operator +(T* x, int y);  
5 T* operator +(T* x, uint y);  
6 T* operator +(T* x, long y);  
7 T* operator +(T* x, ulong y);  
8 T* operator +(int x, T* y);  
9 T* operator +(uint x, T* y);  
10 T* operator +(long x, T* y);  
11 T* operator +(ulong x, T* y);  
12 T* operator -(T* x, int y);  
13 T* operator -(T* x, uint y);  
14 T* operator -(T* x, long y);  
15 T* operator -(T* x, ulong y);  
16 long operator -(T* x, T* y);  
17 ]]></code_example></paragraph>
18   <paragraph>Given an expression P of a pointer type T* and an expression N of type <keyword>int</keyword>, <keyword>uint</keyword>, <keyword>long</keyword>, or <keyword>ulong</keyword>, the expressions P + N and N + P compute the pointer value of type T* that results from adding N * sizeof(T) to the address given by P. Likewise, the expression P  -N computes the pointer value of type T* that results from subtracting N * sizeof(T) from the address given by P. </paragraph>
19   <paragraph>Given two expressions, P and Q, of a pointer type T*, the expression P  -Q computes the difference between the addresses given by P and Q and then divides that difference by sizeof(T). The type of the result is always <keyword>long</keyword>. In effect, P  -Q is computed as ((<keyword>long</keyword>)(P)  -(<keyword>long</keyword>)(Q)) / sizeof(T). </paragraph>
20   <paragraph>
21     <example>[Example: For example: <code_example><![CDATA[
22 using System;  
23 class Test  
24 {  
25    static void Main() {  
26       unsafe {  
27          int* values = stackalloc int[20];  
28          
29          int* p = &values[1];  
30          int* q = &values[15];  
31          
32          Console.WriteLine("p - q = {0}", p - q);  
33          Console.WriteLine("q - p = {0}", q - p);  
34       }  
35    }  
36 }  
37 ]]></code_example>which produces the output: <code_example><![CDATA[
38 p - q = -14  
39 q - p = 14  
40 ]]></code_example>end example]</example>
41   </paragraph>
42   <paragraph>If a pointer arithmetic operation overflows the domain of the pointer type, the result is truncated in an implementation-defined fashion, but no exceptions are produced. </paragraph>
43 </clause>