fixed ステートメント (C# リファレンス)

更新 : 2011 年 4 月

fixed ステートメントは、移動可能な変数がガベージ コレクターにより再配置されることを防ぎます。 fixed ステートメントは、unsafe コンテキストでのみ使用できます。 Fixed を使用して、固定サイズ バッファーを作成することもできます。

fixed ステートメントは、マネージ変数へのポインターを設定し、ステートメントの実行時にマネージ変数を "固定" します。 fixed がない場合、ガベージ コレクションが予期できないかたちで移動可能なマネージ変数を再配置するため、マネージ変数へのポインターはほとんど役に立ちません。 C# コンパイラの場合、fixed ステートメントでは、マネージ変数にポインターを割り当てることだけができます。

unsafe static void TestMethod()
{

    // Assume that the following class exists.
    //class Point 
    //{ 
    //    public int x;
    //    public int y; 
    //}

    // Variable pt is a managed variable, subject to garbage collection.
    Point pt = new Point();

    // Using fixed allows the address of pt members to be taken,
    // and "pins" pt so that it is not relocated.

    fixed (int* p = &pt.x)
    {
        *p = 1;
    }        

}

ポインターは、配列、文字列、固定サイズ バッファー、または変数のアドレスを使用して初期化できます。 次の例は、変数のアドレス、配列、および文字列を使用方法を示しています。 固定サイズ バッファーの詳細については、「固定サイズ バッファー (C# プログラミング ガイド)」を参照してください。

static unsafe void Test2()
{
    Point point = new Point();
    double[] arr = { 0, 1.5, 2.3, 3.4, 4.0, 5.9 };
    string str = "Hello World";

    // The following two assignments are equivalent. Each assigns the address
    // of the first element in array arr to pointer p.

    // You can initialize a pointer by using an array.
    fixed (double* p = arr) { /*...*/ }

    // You can initialize a pointer by using the address of a variable. 
    fixed (double* p = &arr[0]) { /*...*/ }

    // The following assignment initializes p by using a string.
    fixed (char* p = str) { /*...*/ }

    // The following assignment is not valid, because str[0] is a char, 
    // which is a value, not a variable.
    //fixed (char* p = &str[0]) { /*...*/ } 


    // You can initialize a pointer by using the address of a variable, such
    // as point.x or arr[5].
    fixed (int* p1 = &point.x)
    {
        fixed (double* p2 = &arr[5])
        {
            // Do something with p1 and p2.
        }
    }
}

同じ型の場合は、複数のポインターを初期化できます。

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

型の異なるポインターを初期化するには、次の例のように、fixed ステートメントを単純に入れ子にします。

fixed (int* p1 = &point.x)
{
    fixed (double* p2 = &arr[5])
    {
        // Do something with p1 and p2.
    }
}

ステートメントのコードを実行すると、固定された変数の固定が解除され、ガベージ コレクションの対象になります。 そのため、fixed ステートメントの外部にある変数へのポインターは指定しないでください。

注意

fixed ステートメントで初期化されたポインターは変更できません。

unsafe モードでは、スタックにメモリを割り当てることができます。スタックは、ガベージ コレクションの対象にはならないので、固定は必要ありません。 詳細については、「stackalloc (C# リファレンス)」を参照してください。

使用例

    class Point
    { 
        public int x, y; 
    }

    class FixedTest2 
    {
        // Unsafe method: takes a pointer to an int.
        unsafe static void SquarePtrParam (int* p) 
        {
            *p *= *p;
        }

        unsafe 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
     */

C# 言語仕様

詳細については、「C# 言語仕様」を参照してください。 言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

参照

参照

C# のキーワード

unsafe (C# リファレンス)

固定サイズ バッファー (C# プログラミング ガイド)

概念

C# プログラミング ガイド

その他の技術情報

C# リファレンス

履歴の変更

日付

履歴

理由

2011 年 4 月

ポインターの初期化に使用できる型を明記。

カスタマー フィードバック