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.
BEGIN FIXED declaration
statements
END FIXED
declaration | Declaration 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