stackalloc 運算子 (C# 參考)stackalloc operator (C# reference)

stackalloc 運算子會在堆疊上配置記憶體區塊。The stackalloc operator allocates a block of memory on the stack. 在方法執行期間建立的堆疊配置的記憶體區塊,會在該方法傳回時自動被捨棄。A stack allocated memory block created during the method execution is automatically discarded when that method returns. 您無法明確釋放搭配 stackalloc 運算子配置的記憶體。You cannot explicitly free memory allocated with the stackalloc operator. 堆疊配置的記憶體區塊不會被記憶體回收,且不需要以 fixed 陳述式固定。A stack allocated memory block is not subject to garbage collection and doesn't have to be pinned with the fixed statement.

在運算式 stackalloc T[E] 中,T 必須是 unmanaged 型別,且 E 必須是 int 型別的運算式。In expression stackalloc T[E], T must be an unmanaged type and E must be an expression of type int.

您可以將 stackalloc 運算子的結果指派至下列其中一個類型的變數:You can assign the result of the stackalloc operator to a variable of one of the following types:

  • System.Span<T>System.ReadOnlySpan<T> (從 C# 7.2 開始),如下列範例所示:Starting with C# 7.2, System.Span<T> or System.ReadOnlySpan<T>, as the following example shows:

    int length = 3;
    Span<int> numbers = stackalloc int[length];
    for (var i = 0; i < length; i++)
    {
        numbers[i] = i;
    }
    

    當您將堆疊配置的記憶體區塊指派至 Span<T>ReadOnlySpan<T> 變數時,您不需要使用 unsafe 內容。You don't have to use an unsafe context when you assign a stack allocated memory block to a Span<T> or ReadOnlySpan<T> variable.

    當您處理那些類型時,您可以使用條件式或指派運算式中的 stackalloc 運算式,如下列範例所示:When you work with those types, you can use a stackalloc expression in conditional or assignment expressions, as the following example shows:

    int length = 1000;
    Span<byte> buffer = length <= 1024 ? stackalloc byte[length] : new byte[length];
    

    注意

    我們建議盡可能使用 Span<T>ReadOnlySpan<T> 類型來處理堆疊配置的記憶體。We recommend using Span<T> or ReadOnlySpan<T> types to work with stack allocated memory whenever possible.

  • 指標類型,如下列範例所示:A pointer type, as the following example shows:

    unsafe
    {
        int length = 3;
        int* numbers = stackalloc int[length];
        for (var i = 0; i < length; i++)
        {
            numbers[i] = i;
        }
    }
    

    如先前的範例所示,您在使用指標類型時必須使用 unsafe 內容。As the preceding example shows, you must use an unsafe context when you work with pointer types.

新配置記憶體的內容尚未被定義。The content of the newly allocated memory is undefined. 從 C# 7.3 開始,您可以使用陣列初始設定式語法來定義新配置記憶體的內容。Starting with C# 7.3, you can use array initializer syntax to define the content of the newly allocated memory. 下列範例示範進行該操作的數種方法:The following example demonstrates various ways to do that:

Span<int> first = stackalloc int[3] { 1, 2, 3 };
Span<int> second = stackalloc int[] { 1, 2, 3 };
ReadOnlySpan<int> third = stackalloc[] { 1, 2, 3 };

安全性Security

使用 stackalloc 會自動啟用 Common Language Runtime (CLR) 中的緩衝區滿溢偵測功能。The use of stackalloc automatically enables buffer overrun detection features in the common language runtime (CLR). 如果偵測到緩衝區滿溢,會盡快終止處理序,將執行惡意程式碼的機會降到最低。If a buffer overrun is detected, the process is terminated as quickly as possible to minimize the chance that malicious code is executed.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格堆疊配置一節。For more information, see the Stack allocation section of the C# language specification.

另請參閱See also