Jon Jagger
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.2 Pointer types Paragraph 11 In an unsafe context, a type (§11) may be a pointer-type as well as a value-type or a reference-type. type : value-type reference-type pointer-type A pointer-type is written as an unmanaged-type or the keyword void, followed by a * token: pointer-type : unmanaged-type * void * unmanaged-type : type Paragraph 21 The type specified before the * in a pointer type is called the referent type of the pointer type. 2 It represents the type of the variable to which a value of the pointer type points. Paragraph 31 Unlike references (values of reference types), pointers are not tracked by the garbage collector-the garbage collector has no knowledge of pointers and the data to which they point. 2 For this reason a pointer is not permitted to point to a reference or to a struct that contains references, and the referent type of a pointer must be an unmanaged-type. Paragraph 41 An unmanaged-type is any type that isn't a reference-type and doesn't contain reference-type fields at any level of nesting. 2 In other words, an unmanaged-type is one of the following: Paragraph 51 The intuitive rule for mixing of pointers and references is that referents of references (objects) are permitted to contain pointers, but referents of pointers are not permitted to contain references. [Example: Some examples of pointer types are given in the table below:
Example Description
byte* Pointer to byte
char* Pointer to char
int** Pointer to pointer to int
int*[] Single-dimension array of pointers to int
void* Pointer to unknown type
end example]
Paragraph 61 For a given implementation, all pointer types must have the same size and representation. [Note: Unlike C and C++, when multiple pointers are declared in the same declaration, in C# the * is written along with the underlying type only, not as a prefix punctuator on each pointer name. For example:
int* pi, pj;  // NOT as int *pi, *pj;  
end note]
Paragraph 71 The value of a pointer having type T* represents the address of a variable of type T. 2 The pointer indirection operator * (§25.5.1) may be used to access this variable. 3 For example, given a variable P of type int*, the expression *P denotes the int variable found at the address contained in P. Paragraph 81 Like an object reference, a pointer may be null. 2 Applying the indirection operator to a null pointer results in implementation-defined behavior. 3 A pointer with value null is represented by all-bits-zero. Paragraph 91 The void* type represents a pointer to an unknown type. 2 Because the referent type is unknown, the indirection operator cannot be applied to a pointer of type void*, nor can any arithmetic be performed on such a pointer. 3 However, a pointer of type void* can be cast to any other pointer type (and vice versa). Paragraph 101 Pointer types are a separate category of types. 2 Unlike reference types and value types, pointer types do not inherit from object and no conversions exist between pointer types and object. 3 In particular, boxing and unboxing (§11.3) are not supported for pointers. 4 However, conversions are permitted between different pointer types and between pointer types and the integral types. 5 This is described in §25.4. Paragraph 111 A pointer-type may be used as the type of a volatile field (§17.4.3). [Note: Although pointers can be passed as ref or out parameters, doing so can cause undefined behavior, since the pointer may well be set to point to a local variable which no longer exists when the called method returns, or the fixed object to which it used to point, is no longer fixed. For example:
using System;  
class Test  
   static int value = 20;  
   unsafe static void F(out int* pi1, ref int* pi2) {  
      int i = 10;  
      pi1 = &i;  
      fixed (int* pj = &value) {  
         // ...  
         pi2 = pj;  
   static void Main() {  
      int i = 10;  
      unsafe {  
         int* px1;  
         int* px2 = &i;  
         F(out px1, ref px2);  
         Console.WriteLine("*px1 = {0}, *px2 = {1}",  
         *px1, *px2);  // undefined behavior  
end note]
Paragraph 121 A method can return a value of some type, and that type can be a pointer. [Example: For example, when given a pointer to a contiguous sequence of ints, that sequence's element count, and some other int value, the following method returns the address of that value in that sequence, if a match occurs; otherwise it returns null:
unsafe static int* Find(int* pi, int size, int value) {  
   for (int i = 0; i < size; ++i) {  
      if (*pi == value) {  
         return pi;  
   return null;  
end example]
Paragraph 131 In an unsafe context, several constructs are available for operating on pointers:
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS