Show/Hide Toolbars

XSharp

Purpose

The BEGIN FIXED and END FIXED keyword prevent the garbage collector from relocating a movable variable. The BEGIN FIXED statement is only allowed in an unsafe context.

 

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement. Pointers to movable managed variables are useful only in a fixed context. Without a fixed context, garbage collection could relocate the variables unpredictably. The X#  compiler only lets you assign a pointer to a managed variable in a fixed statement.

You can initialize a pointer by using an array, a string, a fixed-size buffer, or the address of a variable.

Syntax

BEGIN FIXED declaration
  statements
END FIXED

Arguments

declarationDeclaration of a variable and assignment that
statements Code including one or more statements that may contain unsafe code.

 

Example

 

UNSAFE FUNCTION Start AS VOID
  VAR s := "SDRS"
  BEGIN FIXED LOCAL p := s AS CHAR PTR
      VAR i := 0
      WHILE p[i] != 0
           p[i++]++
      END
  END FIXED
   Console.WriteLine(s)
   Console.Read()
  RETURN