Export (0) Print
Expand All

A.3 Fixed and moveable variables

Visual Studio .NET 2003

The address-of operator (Section A.5.4) and the fixed statement (Section A.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 (Section A.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 (Section A.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 (Section 7.5.2) that refers to a local variable or a value parameter.
  • A variable resulting from a member-access (Section 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 (Section A.5.1) of the form *P, a pointer-member-access (Section A.5.2) of the form P->I, or a pointer-element-access (Section A.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.

Show:
© 2014 Microsoft