X hits on this document

844 views

0 shares

0 downloads

0 comments

333 / 396

C# LANGUAGE SPECIFICATION

unsafe static int* Find(int* pi, int size, int value) { for (int i = 0; i < size; ++i) { if (*pi == value) return pi; ++pi; } return null; }

In an unsafe context, several constructs are available for operating on pointers:

The * operator may be used to perform pointer indirection (§‎18.5.1).

The -> operator may be used to access a member of a struct through a pointer (§‎18.5.2).

The [] operator may be used to index a pointer (§‎18.5.3).

The & operator may be used to obtain the address of a variable (§‎18.5.4).

The ++ and -- operators may be used to increment and decrement pointers (§‎18.5.5).

The + and - operators may be used to perform pointer arithmetic (§‎18.5.6).

The ==, !=, <, >, <=, and => operators may be used to compare pointers (§‎18.5.7).

The stackalloc operator may be used to allocate memory from the call stack (§‎18.7).

The fixed statement may be used to temporarily fix a variable so its address can be obtained (§‎18.6).

18.3 Fixed and moveable variables

The address-of operator (§‎18.5.4) and the fixed statement (§‎18.6) divide variables into two categories: Fixed variables and moveable variables.

Fixed variables reside in storage locations that are unaffected by operation of the garbage collector. (Examples of fixed variables include local variables, value parameters, and variables created by dereferencing pointers.) On the other hand, moveable variables reside in storage locations that are subject to relocation or disposal by the garbage collector. (Examples of moveable variables include fields in objects and elements of arrays.)

The & operator (§‎18.5.4) permits the address of a fixed variable to be obtained without restrictions. However, because a moveable variable is subject to relocation or disposal by the garbage collector, the address of a moveable variable can only be obtained using a fixed statement (§‎18.6), and that address remains valid only for the duration of that fixed statement.

In precise terms, a fixed variable is one of the following:

A variable resulting from a simple-name (§‎7.5.2) that refers to a local variable or a value parameter.

A variable resulting from a member-access (§‎7.5.4) of the form V.I, where V is a fixed variable of a struct-type.

A variable resulting from a pointer-indirection-expression (§‎18.5.1) of the form *P, a pointer-member-access (§‎18.5.2) of the form P->I, or a pointer-element-access (§‎18.5.3) of the form P[E].

All other variables are classified as moveable variables.

Note that a static field is classified as a moveable variable. Also note that a ref or out parameter is classified as a moveable variable, even if the argument given for the parameter is a fixed variable. Finally, note that a variable produced by dereferencing a pointer is always classified as a fixed variable.

18.4 Pointer conversions

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

Document info
Document views844
Page views844
Page last viewedSun Dec 04 21:28:48 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments