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 须为非托管类型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:

  • 从 C# 7.2 开始的 System.Span<T>System.ReadOnlySpan<T>,如以下示例所示: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 会自动启用公共语言运行时 (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