Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 25 Unsafe codeprevious at this levelnext at this level 25.4 Pointer conversions Paragraph 11 In an unsafe context, the set of available implicit conversions (§13.1) is extended to include the following implicit pointer conversions: Paragraph 21 Additionally, in an unsafe context, the set of available explicit conversions (§13.2) is extended to include the following explicit pointer conversions: Paragraph 31 Finally, in an unsafe context, the set of standard implicit conversions (§13.3.1) includes the following pointer conversion: Paragraph 41 Conversions between two pointer types never change the actual pointer value. 2 In other words, a conversion from one pointer type to another has no effect on the underlying address given by the pointer. Paragraph 51 When one pointer type is converted to another, if the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined if the result is dereferenced. 2 In general, the concept "correctly aligned" is transitive: if a pointer to type A is correctly aligned for a pointer to type B, which, in turn, is correctly aligned for a pointer to type C, then a pointer to type A is correctly aligned for a pointer to type C. [Example: Consider the following case in which a variable having one type is accessed via a pointer to a different type:
char c = 'A';  
char* pc = &c;  
void* pv = pc;  
int* pi = (int*)pv;  
int i = *pi;    // undefined  
*pi = 123456;    // undefined  
end example]
Paragraph 61 When a pointer type is converted to a pointer to byte, the result points to the lowest addressed byte of the variable. 2 Successive increments of the result, up to the size of the variable, yield pointers to the remaining bytes of that variable. [Example: For example, the following method displays each of the eight bytes in a double as a hexadecimal value:
using System;  
class Test  
{  
   static void Main() {  
      double d = 123.456e23;  
      unsafe {  
         byte* pb = (byte*)&d;  
         
         for (int i = 0; i < sizeof(double); ++i)  
         Console.Write(" {0,2:X}", (uint)(*pb++));  
         Console.WriteLine();  
      }  
   }  
}  
Of course, the output produced depends on endianness. end example] Paragraph 71 Mappings between pointers and integers are implementation-defined. [Note: However, on 32-and 64-bit CPU architectures with a linear address space, conversions of pointers to or from integral types typically behave exactly like conversions of uint or ulong values, respectively, to or from those integral types. end note]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS