X hits on this document





332 / 396

Chapter ‎18   Unsafe code

a variable P of type int*, the expression *P denotes the int variable found at the address contained in P.

Like an object reference, a pointer may be null. Applying the indirection operator to a null pointer results in implementation-defined behavior. A pointer with value null is represented by all-bits-zero.

The void* type represents a pointer to an unknown type. 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. However, a pointer of type void* can be cast to any other pointer type (and vice versa).

Pointer types are a separate category of types. Unlike reference types and value types, pointer types do not inherit from object and no conversions exist between pointer types and object. In particular, boxing and unboxing (§‎4.3) are not supported for pointers. However, conversions are permitted between different pointer types and between pointer types and the integral types. This is described in §‎18.4.

A pointer-type may be used as the type of a volatile field (§‎10.4.3).

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 } } }

A method can return a value of some type, and that type can be a pointer. 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:

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.319

Document info
Document views475
Page views475
Page last viewedSun Oct 23 12:12:10 UTC 2016