Export (0) Print
Expand All

fixed Statement

Prevents relocation of a variable by the garbage collector. It takes the following form:

fixed ( type* ptr = expr ) statement

where:

type
An unmanaged type or void.
ptr
A pointer name.
expr
An expression that is implicitly convertible to type*.
statement
Executable statement or block.

Remarks

The fixed statement is only permitted in an unsafe context.

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of statement. Without fixed, pointers to managed variables would be of little use since garbage collection could relocate the variables unpredictably. (In fact, the C# compiler will not allow you to set a pointer to a managed variable except in a fixed statement.)

// assume class Point { public int x, y; }
Point pt = new Point();    // pt is a managed variable, subject to g.c.
fixed ( int* p = &pt.x ){  // must use fixed to get address of pt.x and
    *p = 1;                //   pin pt in place while we use the pointer
}

You can initialize a pointer with the address of an array or a string:

fixed (int* p = arr) ...  // equivalent to p = &arr[0]
fixed (char* p = str) ... // equivalent to p = &str[0]

You can initialize multiple pointers, as long as they are all of the same type:

fixed (byte* ps = srcarray, pd = dstarray) {...}

To initialize pointers of different type, simply nest fixed statements:

fixed (int* p1 = &p.x)
   fixed (double* p2 = &array[5])
      // do something with p1 and p2

Pointers initialized in fixed statements cannot be modified.

After statement is executed, any pinned variables are unpinned and subject to garbage collection. Therefore, do not point to those variables outside the fixed statement.

In unsafe mode, you can allocate memory on the stack, where it is not subject to garbage collection and therefore does not need to be pinned. See stackalloc for more information.

Example

// statements_fixed.cs
// compile with: /unsafe
using System;

class Point { 
   public int x, y; 
}

class FixedTest 
{
   // unsafe method: takes pointer to int
   unsafe static void SquarePtrParam (int* p) 
   {
      *p *= *p;
   }

   unsafe public static void Main() 
   {
      Point pt = new Point();
      pt.x = 5;
      pt.y = 6;
      // pin pt in place:
      fixed (int* p = &pt.x) 
      {
         SquarePtrParam (p);
      }
      // pt now unpinned
      Console.WriteLine ("{0} {1}", pt.x, pt.y);
   }
}

Output

25 6

See Also

C# Keywords | unsafe | Unsafe Code Tutorial | A.6 The fixed statement

Show:
© 2014 Microsoft